【问题标题】:Wordpress display page contents and a loop?Wordpress 显示页面内容和循环?
【发布时间】:2012-11-26 15:41:01
【问题描述】:
我在页面模板中有一个自定义循环,可以通过给定类别和标签按类别显示帖子。它可以工作,但在循环上方我需要显示页面本身的内容,即正常输入到 Wordpress 仪表板的内容。
我要在模板中添加什么来显示此内容?
我试过了:
$id = $post->ID;
get_page($id);
// then my custom loop
获取当前页面 ID,但没有内容。
【问题讨论】:
标签:
php
wordpress-theming
wordpress
【解决方案1】:
在我的情况下,使用现代主题并使用古腾堡块,我必须在帖子循环之前对内容应用过滤器,如以下线程所述:Proper way to get page content
按照其中一个示例,一个简单的可行解决方案是:
<?php
$id = 669; // page ID that has been assigned for posts
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<!-- Enter your custom loop for displaying posts by category down here -->
【解决方案2】:
显示页面内容,使用the_content()函数。
在此函数调用之后添加了您的自定义循环。
【解决方案3】:
在 WordPress 中,调用
<?php the_content(); ?>
将在使用该模板的页面上从所见即所得编辑器中提取内容,只要它在循环内。
最基本的例子可能如下所示:
<?php while (have_posts()) : the_post();/* Start loop */ ?>
<?php the_content(); ?>
<?php endwhile; /* End loop */ ?>
<!-- Enter your custom loop for displaying posts by category down here -->
更多信息在这里:http://codex.wordpress.org/Function_Reference/the_content