• Resolved JackWReid

    (@jackwreid)


    I’ve seen this topic here about how to display the last post from a particular category and I’d like to do this but using categories with slugs rather than IDs. I’ve got this request here:

    <h4 class="center">This Week</h4>
    <?php $cat_id = 0;
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <h4><?php the_title(); ?></h4>	
    
    <?php the_content(); ?>
    
    <?php endwhile; endif; ?>

    I’d like to know how to either find out the ID of categories with slugs switched on, or how to have this request parse a slug. FYI, the page this is going into is actually a specific category template itself.

    Structure:
    root
    – events
    |_ event-reviews
    |_ event-previews
    – listings

    I’m currently altering the category-events template and trying to have it show the last listings post in the sidebar.

    I hope that’s enough context. Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Josh

    (@josh401)

    To use a slug rather than an id; just change category__in to category_name.

    Then, make sure to use a slug variable (named whatever you want) instead of the id variable.

    So, we get this:

    <h4 class="center">This Week</h4>
    <?php $cat_slug = 'my_cat_slug';
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category_name' => $cat_slug));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <h4><?php the_title(); ?></h4>	
    
    <?php the_content(); ?>
    
    <?php endwhile; endif; ?>

    You can learn more about ‘category parameters’ in a WP Query from the Codex.

    Thread Starter JackWReid

    (@jackwreid)

    So I have this code on a page now rather than a category, to simplify matters. It is spitting out posts in the correct quantity but it appears to just be spitting out all posts regardless of category. Any ideas?

    <h4 class="center">This Week</h4>
    <?php $cat_id = 'event-previews';
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__name' => array($cat_id)));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <h6 class="center"><?php the_title(); ?></h6>
    <div class="listings">
    <?php the_content(); ?>
    </div>
    <?php endwhile; endif; ?>
    Josh

    (@josh401)

    Take a closer look at my code.

    'category__name' => array($cat_id)

    Should be…

    'category_name' => $cat_slug

    Also:

    $catid = 'event-previews';

    Should be…

    $cat_slug = 'event-previews';

    It seems you just swapped category__in with category_name… without fully inspecting the differences between the code snippets.

    Thread Starter JackWReid

    (@jackwreid)

    You’re right, all sorted. Thanks for your help!

    Josh

    (@josh401)

    My pleasure!
    Glad you are happy with your results 🙂

    And thanks for marking your topic as resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display latest post from category *with slug*’ is closed to new replies.