如何显示wordpress页面内容?

Dav*_*ave 29 php wordpress function

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

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

我以为是这样的:

<?php echo the_content(); ?>
Run Code Online (Sandbox Code Playgroud)

Dav*_*ave 67

@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; ?>
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了这个,但它显示了所有帖子的内容,但没有显示当前页面. (3认同)
  • 这有效.但是,为什么PHP社区喜欢使用不必要的php标签?阅读真的很混乱,没有必要.如果有兴趣的话,我已经用更清晰的语法发布了这个答案的返工.不是一个仇恨,因为我将赞成这个作为答案. (3认同)

Fre*_*d K 20

这更简洁:

<?php echo get_post_field('post_content', $post->ID); ?>
Run Code Online (Sandbox Code Playgroud)

而这甚至更多:

<?= get_post_field('post_content', $post->ID) ?>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这比预期更难找到. (3认同)

Dan*_*ich 9

对于那些不喜欢看起来很糟糕的代码的人来说,php标签到处爆炸......

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


Nik*_*vic 8

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

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

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


小智 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
?>
Run Code Online (Sandbox Code Playgroud)


per*_*st1 5

页面内容可以通过这种方式轻松完美地显示

<?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; ?>         
Run Code Online (Sandbox Code Playgroud)

笔记:

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

ii) _e() 函数也是可选的,但比仅通过<p>. 而首选的风格化 404.php 可以创建以进行重定向。