WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
Why don't you copy and modify the native recent posts widget? – Brad Dalton Nov 12 '14 at 23:23

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

Try this

        // Get categories
        $categories = wp_get_post_terms( get_the_ID(), 'category');

        // Check if there are any categories
        if( ! empty( $categories ) ) :

            // Get all posts within current category, but exclude current post
            $category_posts = new WP_Query( array(
                'cat'          => $categories[0]->term_id,
                'post__not_in' => array( get_the_ID() ),
            ) );

            // Check if there are any posts
            if( $category_posts->have_posts() ) :
                // Loop trough them
                while( $category_posts->have_posts() ) : $category_posts->the_post();
                    // Display posts
                    echo get_the_title() . '<br />';
                endwhile;
            endif;
        endif;

Also, check out this page for more information about WP_Query for looping through posts, and don't forget to reset the postdata using wp_reset_postdata();

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.