我正在研究WordPress中的新主题,并花费大量时间使用get_the_content()函数。
<div class="clearfix">
<div>
<p><?=get_the_content();?></p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
似乎它不处理快捷方式,也不处理段落。
然后我用the_content()替换了它;我的段落和快捷方式开始起作用。
<div class="clearfix">
<div>
<p><?=the_content();?></p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我的问题:函数之间有什么区别以及需要进行哪些额外的处理the_content(); 与get_the_content();比较?
虽然@J Quest 提供了足够的答案,但我想详细说明一下。一般来说,WordPress 有两种类型的后变量函数:get_函数和the_函数。
get_函数,例如get_the_content()orget_the_ID()将返回所需的信息,然后必须对其进行操作并打印到页面上。一些例子:
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( 'foo', 'bar', $content );
echo 'Post #'. get_the_ID() . $content;
Run Code Online (Sandbox Code Playgroud)
the_功能,如the_content()与the_ID()实际echo返回的值,如果适用的将应用“默认筛选器”为适当的值。这些函数不需要回显。
echo get_the_ID();
Run Code Online (Sandbox Code Playgroud)
在功能上与
the_ID();
Run Code Online (Sandbox Code Playgroud)
如果您查看文档,the_ID()您会发现它实际上只是输出get_the_ID(). 从来源:
function the_ID() {
echo get_the_ID();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果您尝试将the_函数设置为变量,则会在整个页面中留下一串回显变量。
$id = the_ID();
echo 'Post ID: '.$id;
Run Code Online (Sandbox Code Playgroud)
将输出:
123Post ID: 123
Run Code Online (Sandbox Code Playgroud)
要使用get_the_content()并运行短代码,您需要通过do_shortcode()函数运行它,或者更好地进行the_content过滤。
$content = get_the_content();
echo do_shortcode( $content );
// Or:
echo apply_filters( 'the_content', $content );
Run Code Online (Sandbox Code Playgroud)
如果您只需要在模板中吐出帖子内容,而不进行任何操作,通常最好使用(无回声或回声短标签):
the_content();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3768 次 |
| 最近记录: |