Wordpress在窗口小部件中挂钩the_content

时间:2015-11-02 02:22:22

标签: wordpress plugins widget

我正在编写一个小部件,列出帖子中的标题,然后创建哈希链接并编辑HTML以反映这一点。我已经找到了列表小部件内容,我只需要编辑the_content,我试图为返回更新代码的方法添加一个过滤器,但它不起作用。

最好的方法是什么?我的班级名为post_headings_widget,编辑过的HTML内容存储在$this->the_content中。

我希望我能在widget类

中做到这一点
public
function edited_content() {
    return $this->the_content;
}

然后编辑此处的内容输出

add_filter( 'the_content', [ 'post_headings_widget', 'edited_content' ] );

它调用类方法很好,但我不确定它是如何工作的所以我猜它直接调用方法而不调用构造函数等?

我还试图在widget()方法中创建一个过滤器,但这也不起作用,这是我尝试过的:

add_filter( 'the_content', function() {
    return 'test';
} );

关于解决方案的任何想法?

2 个答案:

答案 0 :(得分:0)

您必须在过滤函数/回调中将the_content作为参数传递。

检查Wordpress文档:https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

答案 1 :(得分:0)

在小部件上,您需要绑定到widget_text

add_filter('widget_text', 'se24265_my_function');

function se24265_my_function( $content )
{
# replace code here on widget $content
return $content;
}