2011-03-28 127 views
12

我知道这很简单,但由于某种原因,我只是没有找到我,而Google今天没有帮助我。如何显示wordpress页面内容?

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

我以为是这样的:

<?php echo the_content(); ?> 
+1

大多数WordPress功能只有在“The Loop”里面才能正常工作,http://codex.wordpress.org/The_Loop,特别是如果你想显示内容 – 2011-03-28 22:27:02

+1

另外,你不需要“回声”。 the_content()隐式回声。 – windyjonas 2011-03-28 23:12:45

回答

44

@Marc乙感谢您的评论。帮助我发现这一点:

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

我试过这个,但它显示所有帖子的内容,但不是当前页面。 – Sydney 2016-08-13 14:39:47

+1

这工作。但是,为什么PHP社区喜欢使用不必要的php标签?阅读真的令人困惑,并且没有必要这样做。如果任何人感兴趣,我已经用干净的语法发布了这个答案的返工。不是一个讨厌的人,因为我打算把这个作为回答。 – 2017-07-13 14:14:57

+0

非常有用的帖子 – Student22 2018-03-06 17:32:33

11

这是更简洁:

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

,这甚至更多:

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

谢谢,这比预期更难找到。 – socca1157 2016-08-26 01:50:56

4

只是把这个代码在你的内容的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 
?> 
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()函数是一个可选的使用,如果您需要以启用对不同功能的评论。

ⅱ) _E()函数也是可选的,但不是仅仅示出了通过<p>文本更有意义&有效。而首选的风格化的404.php可以创建重定向。

5

对于那些不喜欢看恐怖代码PHP标签炸开到处人...

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

@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();你在哪里找到最合适的,但是在你调用这个循环之前。