• When we use the standard loop, we can insert a fallback message for when there are no posts available.

    How do I do this with WP_Query?

    Also, if for instance I am querying posts into li tags, where is the best place to drop the opening / closing ul tags so that only show up if posts are queried?

    Rich

Viewing 3 replies - 1 through 3 (of 3 total)
  • How do I do this with WP_Query?

    In exactly the same way you do for a normal Loop:

    <?php
    $the_query = new WP_Query( $args );
    if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
    [ normal Loop stuff ]
    endwhile;
    else:
    [ fallback message ]
    endif;
    ?>

    where is the best place to drop the opening / closing ul tags so that only show up if posts are queried?

    <?php
    $the_query = new WP_Query( $args );
    if ( have_posts() ) : ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
    endwhile;?>
    </ul>
    <?php else:
    [ fallback message ]
    endif;
    ?>
    Thread Starter jrstaatsiii

    (@jrstaatsiii)

    thanks esmi…it turns out that my problem was that I needed to change the main query to get this to work. your code was spot on. thank you.

    Glad I could help 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Fall Back for WP_Query’ is closed to new replies.