如何显示wordpress页面内容?

时间:2011-03-28 22:24:34

标签: php wordpress function

我知道这很简单,但由于某些原因它并没有找到我,谷歌今天也没有帮助我。

我想输出页面内容,我该怎么做?

我以为是这样的:

<?php echo the_content(); ?>

7 个答案:

答案 0 :(得分:62)

@Marc B感谢您的评论。帮我发现了这个:

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

答案 1 :(得分:18)

这更简洁:

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

这甚至更多:

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

答案 2 :(得分:7)

对于那些不喜欢看起来很糟糕的代码的人来说,无处不在的地方爆炸......

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

答案 3 :(得分:7)

@Sydney在调用循环之前尝试放入wp_reset_query()。 这将显示您网页的内容。

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

编辑:如果您之前运行过其他循环,请尝试此操作。 放置wp_reset_query();在哪里找到它最合适,但在你打电话给这个循环之前。

答案 4 :(得分:5)

只需将此代码放入内容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
?>

答案 5 :(得分:3)

页面内容可以轻松显示并且完全这样:

<?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; ?>         

注意:

在显示内容方面 - i)如果您需要启用具有不同功能的评论,则comments_template()函数是可选用的。

ii) _e()函数也是可选的,但更有意义&amp;有效而不只是通过<p>显示文字。虽然可以创建首选的程式化404.php来重定向。

答案 6 :(得分:0)

您可以通过添加此简单的php代码块来实现此目的

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
      the_content();
      endwhile; else: ?>
      <p>!Sorry no posts here</p>
<?php endif; ?>
相关问题