WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i'm using this plugin to show an exapandable/collapsible widget-menu in my sidebar. http://wordpress.org/extend/plugins/folding-category-widget it works very well!

the question, not directly connected with this plugin, is: how can i also show in the list, all posts related to a category (title + a href)?

for example:

category A 

category B

category C

thanks a lot in advance.

share|improve this question
up vote 0 down vote accepted

Well the simplest way I can think of is something like this...

<ul class="categories">
    $categories = get_categories(); //can add parameters here to swtich the order, etc;
        foreach($categories as $i => $category):

<li class="category">
    <span><?php echo $category->name ?></span>


        query_posts('posts_per_page=-1&cat=' . $category->term_id);
        if ( have_posts() ) :
        <ul class="posts">
             while ( have_posts() ) : the_post(); //we make a loop for each category, 
            <li class="post">
                <a href="<?php the_permalink();>"><?php the_title();?></a>
            <?php endwhile; ?>
            <?php endif; ?>
        <?php wp_reset_query(); ?>
        <?php endforeach; endif; ?>

Warning: this is untested code, but it should work just fine. (Just add it to your sidebar.php file or wherever you generate yuor sidebar.)

share|improve this answer
Plus it ought to be said that as the number of categories increase, your going to start racking up a fair few database queries! – TheDeadMedic Dec 14 '10 at 2:20
That's true. Another way to do this would be to just get all the posts and all the cateogries, and for each category, go through the posts array and include them if thery're in that certain category. That would be more database efficient, but you still have to parse the array. – Cronco Dec 14 '10 at 11:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.