php if (have_posts() or ) code example

Example 1: wordpress the loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <h2><?php the_title(); ?></h2>
      <?php the_content(); ?>

    <?php endwhile; else: ?>

      <h2><?php esc_html_e( '404 Error', 'phpforwp' ); ?></h2>
      <p><?php esc_html_e( 'Sorry, content not found.', 'phpforwp' ); ?></p>

<?php endif; ?>

Example 2: have_posts()

NOTE: The functions, have_posts(); and the_post(); are tied to the default automatic query that wordpress makes
on its on using the current URL.

Tags:

Php Example