Display Category Specific Post List in Sidbar ? (1 post)

  1. thornomad
    Posted 9 years ago #


    I am putting together a blog that will have three main categories and, whenever a user is browsing a post from a particular category or the entire category listing in the main loop, I want to display the posts for the viewed category as links in the sidebar. Currently, I have:

    <?php } elseif (is_category() || is_single()) { ?>
    <?php query_posts('cat=XY&showposts=10'); ?>
    <li><h4>Recent Posts</h4></li>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"
    title="permalink for this post">
    <?php the_title(); ?></a></li>
    <?php endwhile;?>
    <?php } else { } ?>

    I need a way for the second line of code where it says cat=XY to allow XY to be a variable that represents whatever category they are currently viewing. (If they are viewing multiple categories, shouldn't show them text at all.)

    I had been looking at the documentation for the_category_ID and the suggested bit of code:

    <?php foreach((get_the_category()) as $cat) {
    echo $cat->cat_ID . ' ';
    } ?>

    However, I don't know how to make it work inside of what I am currently working with.


    PS: I know I could create a seperate if statement for each category and then manually assign the ID tags within each if but that doesn't seem efficient.

Topic Closed

This topic has been closed to new replies.

About this Topic