Wordpress为the_content()添加过滤器


Wordpress add filter to the_content()

我试图添加代码到the_content()函数。我尝试了以下命令

function add_something($content) {
    echo "test";
}
add_filter( 'the_content', 'add_something', 6); 

添加过滤器后,我的帖子只返回没有内容的回显test。如何执行自定义代码而不省略内容?

我猜您需要这样的东西(如果它确实是一个过滤器回调):

function add_something($content) {
    return "test" . $content;
}

似乎是医生说的:http://wordpress.org/support/topic/add-something-to-the_content

您省略了return语句。

function add_something($content) {
echo "test";
... change $content ......
return $content;
}

建议如果要修改内容,必须将其附加到变量后。使用echo将在调用脚本时输出'test'。它不会在the_content()

使用这个(the_content过滤器需要1个参数)

add_filter( 'the_content', 'add_something', 1);