【问题标题】:Wordpress add filter to the_content()Wordpress 将过滤器添加到 the_content()
【发布时间】:2012-01-22 18:03:28
【问题描述】:

我正在尝试向the_content() 函数添加代码。我尝试了以下

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

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

【问题讨论】:

  • 那是你真正的函数声明吗? (见名称前的$)。另外,您为什么不对$content 变量做点什么?仅仅因为你的函数被称为add_ 可能不会告诉 WP 知道它想要附加一些东西。 (实际上它甚至没有这样做。我猜它只是在之前输出。)

标签: php wordpress function filter


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你省略了 return 语句。

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

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

    【讨论】:

    • 谢谢!你的意思是这样吗..$content .= "test"; return $content;
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2012-11-28
      • 1970-01-01
      • 2013-11-07
      • 2021-07-09
      • 2013-06-05
      • 2013-10-17
      • 1970-01-01
      相关资源
      最近更新 更多