Wordpress过滤器get_the_content()

时间:2014-04-25 13:51:10

标签: wordpress filter hook

我正在编写一个需要添加内容的插件;向the_content()添加一个过滤器很简单,但我正在测试的主题使用get_the_content()来构建页面。 有迹象表明它应该可以在Wordpress中使用,但我不明白这个例子:

如果您使用过滤内容的插件(add_filter('the_content')),那么这将不会应用过滤器,除非您以这种方式调用它(使用apply_filters):

apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))

任何人都可以帮忙/解释一下吗?

谢谢,

大卫。

2 个答案:

答案 0 :(得分:1)

他们所说的是您必须将代码添加到get_the_content()被调用的位置。根据您的描述,即主题中的内容 - 您必须按照描述更改主题的get_the_content()调用。

原因是get_the_content()函数中没有可以插入插件的过滤器(请查看the source - 没有调用apply_filters() get_the_content()中的函数,但“更多”链接文本除外。

答案 1 :(得分:0)

迟到了聚会,但是事情就这样了:

在您的 functions.php 中,您可以添加以下内容:

function do_filter_stuff($content) {
    //Whatever you want to do with your content
}
add_filter( 'the_content', 'do_filter_stuff' );

接下来,在您的 page.php (或您要在其中使用的任何模板)中,可以使用以下相同的过滤器调用原始内容:

<?php echo do_filter_stuff(get_the_content()); ?>