• In the example below, how/where can I place ‘else echo ‘<p>(No posts match the criteria.)</p>’;’ code

    <?php
    
    // Set the 'paged' parameter (use 'page' if the query is on a static front page).
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    // Args for the query.
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'paged' => $paged
    );
    
    // The query.
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
    // Paginate (place between if and while).
    echo '<p>' . get_previous_posts_link('&laquo; Newer', $the_query->max_num_pages) . str_repeat('&nbsp;', 2) . get_next_posts_link('Older &raquo;', $the_query->max_num_pages) . '</p>' . "\n\n" ;
    
    echo '<ul>';
    
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
    } // end while
    
    echo '</ul>';
    
    // Paginate (place between while and if).
    echo '<p>' . get_previous_posts_link('&laquo; Newer', $the_query->max_num_pages) . str_repeat('&nbsp;', 2) . get_next_posts_link('Older &raquo;', $the_query->max_num_pages) . '</p>' . "\n\n" ;
    
    } // end if
    
    // reset.
    wp_reset_postdata();
    
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter gulliver

    (@gulliver)

    UPDATE: Sorted (I think)…

    Modified portion of above code is:

    } // end if
    
    else {
    echo '<p>(No posts match the criteria.)</p>';
    }
    
    // reset.
    wp_reset_postdata();

    I normally use a different loop format, and this one is unfamiliar.

    • This reply was modified 6 years, 10 months ago by gulliver.
Viewing 1 replies (of 1 total)
  • The topic ‘Where to place ‘No posts’ else statement.’ is closed to new replies.