我知道这真的很简单,但由于某种原因我没有想到,而且谷歌今天也没有帮助我。
我想输出页面内容,该怎么做?
我以为是这个:
<?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 不应该实现它,并且此时应该弃用它。使用依赖并修改全局状态的随机函数是不好的。这是我找到的最佳替代方案:
<?php
$post = get_queried_object();
?>
<div>
<?php echo do_shortcode(apply_filters('the_content', $post->post_content)); ?>
</div>
所有答案似乎都是在页面上完成的,感觉相当快速和肮脏。您可能想重用它,那么为什么不添加一个简单的页面模板呢?
将以下内容保存到 PHP 文件中,例如
cleanpage.php
并将其上传到您的wp-content/themes/WhatEverThemeYouAreUsing
目录。
我刚刚在我的网络服务器上的上述目录中打开
nano
,并将所有内容复制到其中。
<?php
/**
* Template Name: Clean Page
* This template will only display the content you entered in the page editor
*/
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body class="cleanpage">
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php wp_footer(); ?>
</body>
</html>
然后您可以在页面上选择它作为模板,如下所示: