Support » Fixing WordPress » How to show posts from same category as current post in sidebar?

  • sysbuilder

    (@sysbuilder)


    Hi,

    I am moderately experienced in PHP but am a newbie when it comes to WordPress functions.

    I was wondering if someone could point me in the right direction in terms of which WordPress functions to look into to implement the following setup:

    When viewing the home page, I would like the sidebar to display links to recent posts. However, when viewing a category page or a single post, I would like the sidebar to show recently posted posts from that specific category.

    Any help or insight would be greatly appreciated! Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Len

    (@lenk)

    Place the following in sidebar.php or whatever file your theme uses to display the sidebar,

    <?php global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <h3>More News From This Category</h3>
    <ul>
    <?php
    $posts = get_posts('numberposts=20&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    <li><strong><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">ARCHIVE FOR '<?php echo $category->name; ?>' CATEGORY &raquo;</a></strong></li>
    <?php endforeach; ?>
    </ul>

    This will display the titles of the most 20 recent entries of whatever category the post displayed on single.php belongs to.

    In other words, someone arrives at the home page of your blog. They start reading a post about cars that belongs to the Automobile category. The click the title (or read more link or whatever) and arrive at the single post page (single.php) to finish reading the article. In the sidebar will be a list of the 20 most recent posts belonging to the Automobile category.

    To change the number of titles displayed simply alter numberposts=20

    Thread Starter sysbuilder

    (@sysbuilder)

    Awesome.. tweaked the code a bit and it works great.

    One more question – I tried adding the_excerpt() so it looked like this (as I’d like to have a short excerpt of each entry that gets displayed in the sidebar):

    `<?php
    global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <h2><?php echo $category->name; ?></h2>
    <?php
    $posts = get_posts(‘numberposts=20&category=’. $category->term_id);
    foreach($posts as $post) :
    ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>
    <?php endforeach; ?>`

    When I do that, the same excerpt gets shown for all of the entries, although the entry titles change.

    Any idea of what I might be doing wrong?

    You need to include setup_postdata($post) to give get_posts access to some loop functions

    <?php foreach ($posts as $post) :
             setup_postdata($post); ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    I know that allows get_posts to use the_content(). Can’t recall whether it gets the_excerpt working too. If not try
    echo $post->post_excerpt

    see http://codex.wordpress.org/Template_Tags/get_posts

    Is there a way to omit the recent post from the sidebar which currently viewing?

    frozen-banana

    (@frozen-banana)

    Thanks for the code! I have very limited php experience and am running into a small problem. On a category page if the first post belongs to three categories I’ll get a list of posts for each category instead of only the list of posts for the current category. Don’t know enough about php to see if there is an obvious reason why it’s paying more attention to the post’s category than the page’s category. Is there any way to alter the code so it only shows posts from the current category instead?

    Thanks a ton!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show posts from same category as current post in sidebar?’ is closed to new replies.