我正在使用WordPress中的新主题,并花了很多时间使用get_the_content()函数.
<div class="clearfix">
<div>
<p><?=get_the_content();?></p>
</div>
</div>
似乎它不处理快捷方式而不执行段落.
然后我用the_content()替换它;我的段落和快捷方式开始奏效.
<div class="clearfix">
<div>
<p><?=the_content();?></p>
</div>
</div>
我的问题:函数和附加处理之间的区别是什么?_content();与get_the_content()进行比较;?
解决方法:
虽然@J Quest提供了足够的答案,但我想详细说明一下.一般来说,WordPress有两种类型的post变量函数:get_function和the_ functions.
get_function,例如get_the_content()
或get_the_ID()
将返回所需的信息,然后必须对其进行操作并打印到页面.一些例子:
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( 'foo', 'bar', $content );
echo 'Post #'. get_the_ID() . $content;
the__function(例如the_content()
和the_ID()
)实际上回显返回的值,如果适用,将对适当的值应用“默认过滤器”.这些功能不需要回显.
echo get_the_ID();
在功能上是相同的
the_ID();
如果你查看the_ID()的文档,你会发现它只是输出get_the_ID()的值.从来源:
function the_ID() {
echo get_the_ID();
}
在这种情况下,如果您尝试将the_ functions设置为变量,您将在整个页面中留下回显变量的踪迹.
$id = the_ID();
echo 'Post ID: $id';
将输出:
123Post ID: 123
要使用get_the_content()并获取要运行的短代码,您需要通过do_shortcode()
函数或更好的the_content过滤器运行它.
$content = get_the_content();
echo do_shortcode( $content );
// Or:
echo apply_filters( 'the_content', $content );
如果您只是需要在模板中吐出帖子内容而不进行任何操作,通常情况下会更好(没有回声或回声短标记):
the_content();
原文地址:https://codeday.me/bug/20190910/1800638.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。