• Resolved scottoliphant

    (@scottoliphant)


    The post title says it all. Been searching for the past hour trying to figure this out. I want to display a list of the post in the sidebar for the current category. I need to do this dynamically, not by creating a single-xx.php page. Any help is super appreciated, thanks in advance everyone!
    scott

Viewing 3 replies - 1 through 3 (of 3 total)
  • Here is one solution for your sidebar. It should work for most themes and situations. There are some limitations of this method mostly having to do with it not rewinding the post counter on multi-loop pages.

    Just stick the code in the sidebar, change the number of posts per category displayed and give it a shot. There are other ways to accomplish this and some different ways of handling multiple categories but this should give you a good start:

    //if on single page
    if ( is_single() ) :
    //setup post data
    global $post;
    //get the category(s) of current post
    $categories = get_the_category();
    //loop through each category the post belongs too
    foreach ($categories as $category) :
    ?>
    //print category title
    <li class="sidebar_links">
    <h2>Recent <?php echo $category->name; ?> Posts</h2>
    
    <ul>
    <?php
    //how many other posts from each of the current posts category
    $posts = get_posts('numberposts=5&category='. $category->term_id);
    //loop through posts
    foreach($posts as $post) :
    ?>
    
    //print post links
    <li>
    <a href="<?php the_permalink(); ?>" class="sidebar_link"><?php the_title(); ?>
    </a>
    </li>
    //end posts loop
    <?php endforeach; ?>
    </ul>
    
    </li>
    <?php
    //end categories loop
    endforeach; endif ; ?>

    -Aaron
    wpcast.com (coming soon)

    Thread Starter scottoliphant

    (@scottoliphant)

    Hey thanks Aaron! It got me close, ended up going with this, will incorporate your idea for post number as well. thank you so much.

    <h3>Archives</h3> <ul>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <li>
    <?php the_date(); ?> // <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a><br />
    <?php //the_excerpt(); ?>
    </li><br />
    <?php endwhile; ?>
    <?php endif; ?>

    Glad you got it working.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List archive of posts in sidebar of current category (dynamically)’ is closed to new replies.