【问题标题】:Wordpress Hook the_content Within WidgetWordpress 在小部件中挂钩 the_content
【发布时间】:2016-02-01 20:58:57
【问题描述】:

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

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

我希望我可以在小部件类中做到这一点

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';
} );

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

【问题讨论】:

    标签: wordpress plugins widget


    【解决方案1】:

    在你需要绑定widget_text的小部件上

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 2010-11-26
        • 2022-08-03
        • 2018-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多