• I’m trying to show either the most recent post from the category ‘test’ if one exists and if there are no posts in that category the most recent post is shown (from any category).

    The code below works, I wanted to know if there’s a better or more correct way to achieve this?

    <?php $recent = new WP_Query("category_name=test&showposts=1");
    if(!$recent->have_posts()) {$recent = new WP_Query("showposts=1");}
    while($recent->have_posts()) : $recent->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
    <?php endwhile; wp_reset_query(); ?>

  • The topic ‘If no posts in one category then show most recent post’ is closed to new replies.