我正在研究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>
我的问题:函数之间有什么区别以及需要进行哪些额外的处理the_content(); 与get_the_content();比较?
尽管@J Quest提供了适当的答案,但我想详细说明一下。一般来说,WordPress具有两种类型的后变量函数:get_
函数和the_
函数。
get_
功能,例如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_
函数,例如返回值the_content()
和the_ID()
实际上echo
返回的值,如果适用,将对适当的值应用“默认过滤器”。这些功能不需要回显。
echo get_the_ID();
在功能上与
the_ID();
如果您查看文档,the_ID()
就会发现它只是输出的值get_the_ID()
。从来源:
function the_ID() {
echo get_the_ID();
}
因此,如果您尝试将the_
函数设置为变量,则会在整个页面中留下回显变量的痕迹。
$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 );
如果您只需要在模板中吐出帖子内容,而无需进行任何操作,通常最好使用(没有echo或echo short标签):
the_content();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句