How do I display a wordpress page content?

191,782

Solution 1

@Marc B Thanks for the comment. Helped me discover this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

Solution 2

This is more concise:

<?php echo get_post_field('post_content', $post->ID); ?>

and this even more:

<?= get_post_field('post_content', $post->ID) ?>

Solution 3

@Sydney Try putting wp_reset_query() before you call the loop. This will display the content of your page.

<?php
    wp_reset_query(); // necessary to reset query
    while ( have_posts() ) : the_post();
        the_content();
    endwhile; // End of the loop.
?>

EDIT: Try this if you have some other loops that you previously ran. Place wp_reset_query(); where you find it most suitable, but before you call this loop.

Solution 4

For people that don't like horrible looking code with php tags blasted everywhere...

<?php
if (have_posts()):
  while (have_posts()) : the_post();
    the_content();
  endwhile;
else:
  echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>

Solution 5

Just put this code in your content div

<?php
// TO SHOW THE PAGE CONTENTS
    while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
        <div class="entry-content-page">
            <?php the_content(); ?> <!-- Page Content -->
        </div><!-- .entry-content-page -->

    <?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>
Share:
191,782
Dave
Author by

Dave

Updated on September 18, 2020

Comments

  • Dave
    Dave over 3 years

    I know this is really simple but it just isn't coming to me for some reason and google isn't helping me today.

    I want to output the pages content, how do I do that?

    I thought it was this:

    <?php echo the_content(); ?>
    
  • Sydney
    Sydney over 7 years
    I tried this but it display the content of all the posts but not the current page.
  • socca1157
    socca1157 over 7 years
    Thanks, this was harder to find than expected.
  • Dan Zuzevich
    Dan Zuzevich almost 7 years
    This works. However, why is it that the PHP community loves to use unnecessary amounts of php tags? It is really confusing to read, and there is no need for it. I have posted a rework of this answer with cleaner syntax if anyone is interested. Not being a hater, as I am going to upvote this one as the answer.
  • I am the Most Stupid Person
    I am the Most Stupid Person over 5 years
    Last one works only if you have enable PHP shortcodes
  • Khom Nazid
    Khom Nazid about 5 years
    While the usual the_content() is correct, in some custom post templates what you suggest is much better. Doesn't need the wp_query_reset. Thank you!