How do I display a wordpress page content?

前端 未结 7 683
悲&欢浪女
悲&欢浪女 2021-01-31 03:08

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?

相关标签:
7条回答
  • 2021-01-31 03:37

    You can achieve this by adding this simple php code block

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
          the_content();
          endwhile; else: ?>
          <p>!Sorry no posts here</p>
    <?php endif; ?>
    
    0 讨论(0)
  • 2021-01-31 03:42

    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;
    ?>
    
    0 讨论(0)
  • 2021-01-31 03:42

    Page content can be displayed easily and perfectly this way:

    <?php if(have_posts()) : ?>
        <?php while(have_posts())  : the_post(); ?>
          <h2><?php the_title(); ?></h2>                        
          <?php the_content(); ?>          
          <?php comments_template( '', true ); ?> 
        <?php endwhile; ?>                   
          <?php else : ?>                       
            <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
    <?php endif; ?>         
    

    Note:

    In terms of displaying content - i) comments_template() function is an optional use if you need to enable commenting with different functionality.

    ii) _e() function is also optional but more meaningful & effective than just showing text through <p>. while preferred stylized 404.php can be created to be redirected.

    0 讨论(0)
  • 2021-01-31 03:45

    @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; ?>
    
    0 讨论(0)
  • 2021-01-31 03:48

    @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.

    0 讨论(0)
  • 2021-01-31 03:51

    This is more concise:

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

    and this even more:

    <?= get_post_field('post_content', $post->ID) ?>
    
    0 讨论(0)
提交回复
热议问题