WordPress.org

Ready to get started?Download WordPress

Forums

displaying posts from a single category in sidebar (7 posts)

  1. oldbag66
    Member
    Posted 5 years ago #

    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.

  2. oldbag66
    Member
    Posted 5 years ago #

    43 minutes later i found the answer here:

  3. oldbag66
    Member
    Posted 5 years ago #

    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>
  4. kapiljain.in
    Member
    Posted 5 years ago #

    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>
  5. oldbag66
    Member
    Posted 5 years ago #

    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>
  6. oldbag66
    Member
    Posted 5 years ago #

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

  7. redinbos
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic