WordPress.org

Support

Support » Themes and Templates » displaying posts from a single category in sidebar

displaying posts from a single category in sidebar

  • When a single post from a category is viewed (single.php) i want in the side bar to show latest 20 posts titles from the same category as the single post is in, im using conditional statements in the side and and not widgets.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 43 minutes later i found the answer here:

    ok i got this code in the side bar BUT its no good if the main post is in more than one catagory, where catagory name is i need it to show ALL the catgorys for the post that has been selected

    <?php global $post; $categories = get_the_category();foreach ($categories as $category) : ?>
    <h2>&raquo; <?php echo $category->name; ?></h2>
    <ul class="rightcol">
    <?php $posts = get_posts('numberposts=20&amp;category='. $category->term_id);foreach($posts as $post) :?>
    <li><span class="threecolsmall"><?php the_time('j M y') ?></span><br />
    <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>

    I have made changes in the above code, find the same. I think it works for you:

    <?php global $post; $categories = get_the_category();foreach ($categories as $category) : ?>
    <h2>&raquo; <?php echo $category->name; ?></h2>
    <ul class="rightcol">
    
    <?php
    //Code added for getting all categories in a variable $cat_ids
    foreach((get_the_category()) as $category) {
    	$cat_ids=$cat_ids . $category->cat_ID . ',';
    }
    $cat_ids=substr($cat_ids, 0, -1);
    //----------
    ?>
    
    <?php $posts = get_posts('numberposts=20&amp;category='. $cat_ids);foreach($posts as $post) : //replaced $category->term_id with $cat_ids ?>
    <li><span class="threecolsmall"><?php the_time('j M y') ?></span><br />
    <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>

    hi, further to this idea i have the problem if the post is in one or more category it displays a list within a list, ideally i just want to show all the catagories the post is in but ONLY the other posts in the catagory it was selected…hope that makes sense!

    <?php global $post; $categories = get_the_category();foreach ($categories as $category) : ?>
    <h2>&raquo; <?php echo $category->name; ?></h2>
    <p><?php _e('Category: ', 'blank'); ?><?php the_category(', ') ?></p>
    <ul class="rightcol">
    <?php $posts = get_posts('numberposts=20&amp;category='. $category->term_id);foreach($posts as $post) :?>
    <li><span class="threecolsmall"><?php the_time('j M y') ?></span><br />
    <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>

    .. sorry i ment more than one category, if its just one its not a problem!

    is there a way I can display a sub category and not the parent category….

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘displaying posts from a single category in sidebar’ is closed to new replies.
Skip to toolbar