Problems with using variables in php
-
I’m having problems with a piece of code that sits in the sidebar of my posts. The code is supposed to get the category parent ID, assign it to a variable, and then use that variable when calling for the most recent posts from the category.
<?php foreach((get_the_category()) as $category) { $parent_id = $category->category_parent . ' '; }; ?> <?php echo $parent_id; ?> <?php if(is_single()) { $my_query = new WP_Query('cat=$parent_id&order=desc&showposts=7'); ?> <h2>Related Stories</h2> <?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <br /> <?php endwhile; } ?>The cat=%parent_id bit doesn’t seem to work (after $my_query = new WP_Query). When I replace %parent_id with (for example) 3, it correctly displays posts with category id 3. When I echo %parent_id it prints the correct category of the post, (for example – 3).
So there’s really no reason why it shouldn’t be working, unless there’s something obvious I’ve missed. Any help will be very gratefully received. Thank you.
The topic ‘Problems with using variables in php’ is closed to new replies.