挂接在WordPress的the_content过滤器(Hooking the_content fi

2019-07-29 08:27发布

我节省了帖子内容变成一元,我想找回它代替了原来的帖子内容的,这样,当我打电话the_content()出现在后的元数据而不是实际的POST数据。

function test(){
    $post_meta = post meta data here ....
    echo apply_filters('the_content', '$post_meta');
}
add_filter('the_content', 'test');

我得到这个错误

Fatal error: Maximum function nesting level of '100' reached

错误有一定道理,但我怎么能做到什么,我试图做的,任何想法?

Answer 1:

更新:多敲我的头撞在墙上之后,这里是我能想到的挂接到the_content并使用其过滤器从自定义回调中不陷入无限循环的最佳方式。

答案是惊人的简单,我觉得傻之前没有想到的是:

function test($content)
{
    remove_action('the_content', 'test'); //DISABLE THE CUSTOM ACTION
    $post_meta = post meta data here ....
    $cleaned_meta = apply_filters('the_content', $post_meta);
    add_action('the_content', 'test'); //REENABLE FROM WITHIN
    return $cleaned_meta;
}
add_action('the_content', 'test');

我敢肯定,你已经找到了另一种解决方案到现在,不过,我希望这有助于你可能会遇到在未来的任何问题。



Answer 2:

我想你想之前添加过滤the_content()甚至被称为。 你可能希望这样的事情。

function modify_content() {
    global $post;
    $post_meta = 'testing';
    $post->post_content = $post_meta;
}
add_action('wp_head', 'modify_content');

这将允许你修改后的内容,并仍然通过过滤器上运行它the_content() 虽然这将仅适用于单个职位/网页的工作。 你将不得不寻找另一个钩子,如果你想修改存档页面了。



文章来源: Hooking the_content filter in wordpress
标签: php wordpress