WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] In sidebar list all posts in this post's category (8 posts)

  1. holney
    Member
    Posted 6 years ago #

    Hi

    In my header I have a horizonal bar listing categories.

    In my sidebar I list all posts in the selected category.

    This works fine when I'm on a category page but when I go to a single post page, only that post is listed in the sidebar.

    I would like some way of listing all the posts that belong to the current post's category in the sidebar.

    Posts will only ever be in one category.

    Any help would be much appreciated.

    Thanks

  2. moshu
    Member
    Posted 6 years ago #

    Paste your sidebar code to http://wordpress.pastebin.ca and post back the url.

  3. holney
    Member
    Posted 6 years ago #

    Thanks Moshu - here it is

    http://wordpress.pastebin.ca/993383

  4. moshu
    Member
    Posted 6 years ago #

    THat seems to be the same as the main loop in your whatever template is used (index or archive) and it will not work: the two Loops interfere.

    I'd suggest to read about using multiple loops.

  5. holney
    Member
    Posted 6 years ago #

    Hi Moshu

    I haven't changed the code from what it was in the original template. I was just wondering whether it was technically possible or whether there was a widget or plugin. I'll read the information you suggested.

    Many thanks

  6. moshu
    Member
    Posted 6 years ago #

    It is possible, of course.

    I haven't changed the code from what it was in the original template.

    That's exactly the problem.
    When the template (archive.php or category.php, whichever you have) is showing XY number of posts... your sidebar just replicates that.
    When the single.php is showing only ONE post... your sidebar again replicates it, since it is using exactly the same Loop.

    This is the reason I suggested to read about using multiple Loops.

  7. holney
    Member
    Posted 6 years ago #

    Many thanks again moshu. This is fixed now. Here's my code if anyone else wants to do the same thing:

    <h3>Posts</h3>
    		<ul>
    
                    <?php while(have_posts()) : the_post(); ?>
    
    		<?php foreach((get_the_category()) as $category)
                              { $my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');} ?> 
    
                           <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    		       <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
                          <?php endwhile; ?>
    
     		<?php break; endwhile; ?>
    
    		</ul>
  8. Indojepang
    Member
    Posted 5 years ago #

    Many thanks holney this solve my problem :D
    one thing though.. how can I exclude the active post?

Topic Closed

This topic has been closed to new replies.

About this Topic