Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a modified version of Twenty Twelve and I'm trying to display recent posts from the same category as the current post (except the current post) in the sidebar.

I started this way but something's not ok:

    $category = get_the_category($post->ID); 
    $current_cat = $category[0]->cat_name; //This will get me the first category assigned to the current post but since every post has only ONE cat. assigned to, it's good enough
   //Next, in my sidebar widget I have this:
    $my_query = new WP_Query('category_name='.$current_cat.'&showposts=10');
    while ($my_query->have_posts()) : $my_query->the_post();
    ...display my titles here ...
    unset($current_cat); //I'm not sure I need to unset this variable?
    endwhile;

I'm not really a programmer so I'm struggling with understanding the logic but I'd like to learn. Any suggestions/help much appreciated!

Thank you, Alex

share|improve this question

1 Answer 1

Try this way:

global $post;
$category = get_the_category($post->ID); 
$current_cat = $category[0]->cat_name; //This will get me the first category assigned to the current post but since every post has only ONE cat. assigned to, it's good enough

$my_query = new WP_Query('category_name='.$current_cat.'&showposts=10');
while ($my_query->have_posts()) : $my_query->the_post();
    the_title();
unset($current_cat); //I'm not sure I need to unset this variable?
endwhile;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.