我当时正在研究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>
我的问题是:与get_the_content();相比,这些函数之间有什么区别,the_content();有哪些额外处理?
发布于 2018-06-17 11:21:31
虽然@J Quest提供了一个足够的答案,但我想详细说明一下。一般来说,WordPress有两种类型的post变量函数:get_
函数和the_
函数。
get_the_content()
或get_the_ID()
等get_
函数将返回所需的信息,然后必须对其进行操作并将其打印到页面。下面是一些例子:
$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 );
如果你只需要在模板中输出post内容,而不需要任何操作,你通常会更好地使用(没有echo或echo短标签):
the_content();
发布于 2018-06-17 08:42:04
get_the_content()
不通过the_content
传递内容。这意味着它不会自动嵌入视频,也不会扩展短码等。
只需使用get_the_content()
,它就会删除这些标记。
https://codex.wordpress.org/Function_Reference/get_the_content https://developer.wordpress.org/reference/functions/the_content/
https://stackoverflow.com/questions/50892776
复制相似问题