Get Latest Post from Category's Permalink? (5 posts)

  1. dudleyi
    Posted 4 years ago #

    I want to have a bit of php code that I can place anywhere that refers to the latest post in a particular category. How would I go about that?

    I'm a bit mystified about how to do this.I looked around the codex and couldn't find a clearcut answer.


  2. Anna
    Posted 4 years ago #

    You can use query_posts for something like that.

    posts_per_page is set to get the latest 1 post and cat=1 limits the post to being from that specific category.

    <?php query_posts('posts_per_page=1&cat=1');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    endwhile; else:
    wp_reset_query(); ?>
  3. brisimmons105
    Posted 4 years ago #

    Hi OTN,

    I know this is a bit late, but is there a way to do this without knowing the specific category ID?

    I'd like to have the category ID dynamically generated, since I will have lots of categories.

    I'm guessing I need a loop to do this, but I am not sure.


  4. chriseverson
    Posted 4 years ago #

    Could you provide more detail about the usage? There are various different ways to pull category IDs dynamically.

    Specifically, are you pulling just one category at a time, all categories, or a selection of categories?

  5. brisimmons105
    Posted 4 years ago #

    Thanks Chris,

    Basically, I have lots of categories that act as bands/musicians, and I will be adding more each week.
    Each post in the category will represent a song by that band...so there may anywhere from 1-10 posts in that category.

    Now I'm currently listing out all of the bands I have on an 'artists' page, which basically just calls all of the categories on my site at one time, and lists them out.

    When a user clicks on a band name, right now it takes them to the category archive page that lists out all of the posts in that category....which I don't want to happen.

    I'd like it to skip the category archives page all together and go right to the latest post in that category....

    Does that help?

Topic Closed

This topic has been closed to new replies.

About this Topic