Support » Themes and Templates » Get Latest Post from Category's Permalink?

  • 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.

    Thanks
    Dudley

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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:
     ..
    endif;
    
    wp_reset_query(); ?>

    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.

    Thanks.

    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?

    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get Latest Post from Category's Permalink?’ is closed to new replies.