php 如何显示 wordpress 页面内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5465765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:31:53  来源:igfitidea点击:

How do I display a wordpress page content?

phpwordpressfunction

提问by Dave

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

回答by Dave

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

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

回答by Fred K

This is more concise:

这更简洁:

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

and this even more:

这甚至更多:

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

回答by Dan Zuzevich

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

对于那些不喜欢到处都是带有 php 标签的可怕代码的人......

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

回答by Nikola Jovanovic

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

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

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

编辑:如果您之前运行过其他一些循环,请尝试此操作。放置 wp_reset_query(); 在您认为最合适的地方,但在调用此循环之前。

回答by pnkj

Just put this code in your content div

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

回答by perfectionist1

Page content can be displayed easilyand 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.

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

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.

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