php Wordpress 向 the_content() 添加过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8547255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:57:52  来源:igfitidea点击:

Wordpress add filter to the_content()

phpwordpressfunctionfilter

提问by CyberJunkie

I'm trying to add a code to the_content()function. I tried the following

我正在尝试添加代码以the_content()发挥作用。我尝试了以下

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

After adding the filter my posts return just the echo testwithout the content. How can I execute my custom code without omitting the content?

添加过滤器后,我的帖子只返回test没有内容的回声。如何在不忽略内容的情况下执行我的自定义代码?

回答by mario

I would surmise you need something like that (if it's indeed a filter callback):

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

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

Seems what the docs say: http://wordpress.org/support/topic/add-something-to-the_content

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

回答by Friso Kluitenberg

you omitted the return statement.

你省略了 return 语句。

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

}

Be advised that if you want to modify the content, you must append it to the variable. Using echo will output 'test' at the time the script is called. It will not append or prepend it to the_content()

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