WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add list of posts to page sidebar belonging to special category name (8 posts)

  1. Jeremy Herve
    Member
    Posted 4 years ago #

    Hi!

    I am having a small issue setting up a list of posts in a sidebar.

    Let me try to explain you what I'd like to do!

    On my website, I have pages that have the same slug name than the post categories (ex: one of my pages is called artist-1, and I have got a category called artist-1)

    I'd like to use this similarity in a specific sidebar for pages, where I would call a list of the 5 latest posts belonging to the category matching the page name slug.
    e.g. on the page artist-1, I'd like to show in the sidebar a list of the 5 latest articles belonging to category artist-1.

    Of course, I don't want to modify the code every time I add a new category and a new page.

    Any idea, somebody?

  2. Jeremy Herve
    Member
    Posted 4 years ago #

    I figure the beginning should be like so:

    <ul>
    <?php	query_posts( 'category_name=XXXX&showposts=5' );
    	while (have_posts()) : the_post();
    ?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
    </ul>

    But I dont know what to put instead of the XXXX.

    I should find a function calling the actual page slug.

    Any idea?

  3. bmg1227
    Member
    Posted 4 years ago #

    Try something like this:

    <?php if (is_page(1)) { ?>
    <ul>
    <?php $recent = new WP_Query("cat=1&showposts=5"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
    </ul>
    <?php } elseif (is_page(2)) { ?>
    <ul>
    <?php $recent = new WP_Query("cat=2&showposts=5"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
    </ul>
    <?php } else { ?>
    PUT HERE SOMETHING IF IT'S NOT ANY OF THE ABOVE PAGES
    <?php } ?>

    The code above will basically show a list of most recent posts for category ID #1 if you are viewing page ID #1, and category ID #2 if you are viewing page ID #2 and so on...simply rinse and repeat.

    Is this what you were looking for?

  4. MichaelH
    Member
    Posted 4 years ago #

    Looks like you could use $post->post_name

    <?php
        $args=array(
          'category_name' => $post->post_name
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '5 recent category Posts ';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. Jeremy Herve
    Member
    Posted 4 years ago #

    Thanks to both of you!

    I will have a look to both solutions, and come back to tell you which one is better for me! :)

  6. Jeremy Herve
    Member
    Posted 4 years ago #

    OK, I have tried both, I think I will choose Michael's solution, like that I won't have to add ines of code when my client wants to create a new page and a new category.

    Just for info Michal, your code misses a , at the end of line 3. I was wondering what was not ok after I tried your code and realized that. Code with the additional , would be like so:

    <?php
        $args=array(
          'category_name' => $post->post_name,
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '5 recent category Posts ';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thank you again for your help!!

  7. MichaelH
    Member
    Posted 4 years ago #

    Just for info Michal, your code misses a ,

    That's what I get for throwing code in without testing. Thanks.

  8. HarryByme
    Member
    Posted 4 years ago #

    Thanks all,

    This post solved my issue where I want to display a categorised post based on day of week.

Topic Closed

This topic has been closed to new replies.

About this Topic