[英]How do I display a wordpress page content?
我知道这真的很简单,但出于某种原因我并没有想到它,谷歌今天也没有帮助我。
我想要 output 页面内容,我该怎么做?
我以为是这样的:
<?php echo the_content(); ?>
@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; ?>
这更简洁:
<?php echo get_post_field('post_content', $post->ID); ?>
这甚至更多:
<?= get_post_field('post_content', $post->ID) ?>
@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(); 在您认为最合适的地方,但在调用此循环之前。
对于那些不喜欢到处都是带有 php 标签的可怕代码的人......
<?php
if (have_posts()):
while (have_posts()) : the_post();
the_content();
endwhile;
else:
echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>
只需将此代码放在您的内容 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
?>
页面内容可以通过这种方式轻松完美地显示:
<?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: Not Found'); ?></h3>
<?php endif; ?>
笔记:
在显示内容方面 - i)如果您需要启用具有不同功能的评论,则comments_template() 函数是一个可选用途。
ii) _e() 函数也是可选的,但比仅通过<p>
显示文本更有意义和有效。 而首选的风格化 404.php 可以创建以进行重定向。
您可以通过添加这个简单的 php 代码块来实现这一点
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>!Sorry no posts here</p>
<?php endif; ?>
“循环”是不好的做法。 WordPress 应该从未实现过它,此时应该弃用它。 依赖全局 state 变量是不好的。 这是我找到的最佳选择:
<?php
$post = get_queried_object();
?>
<div>
<?php echo do_shortcode(apply_filters('the_content', $post->post_content)); ?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.