在 wordpress 中挂钩 the_content 过滤器 [英] Hooking the_content filter in wordpress

查看:27
本文介绍了在 wordpress 中挂钩 the_content 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将帖子内容保存到帖子元数据中,我想检索它而不是原始帖子内容,这样当我调用 the_content() 时,帖子元数据中的数据就不会出现实际的帖子数据.

I'm saving the post content into a post meta, and I'd like to retrieve it instead of the original post content so that when I call the_content() the data in the post meta appears not the actual post data.

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

这个错误确实有道理,但是我怎样才能实现我想要做的事情,有什么想法吗?

The error does make sense, but how can I achieve what I'm trying to do, any ideas?

推荐答案

更新: 经过我的头撞墙,这是我能想到的最好的方法来挂钩 the_content 并使用它从自定义回调中过滤,而不会陷入无限循环.

UPDATE: After much banging my head against the wall, here is the best way I can think of to hook into the_content and use its filter from within the custom callback without falling into an infinite loop.

答案出奇的简单,之前没想到我觉得很傻:

The answer was surprisingly simple, and I feel silly for not thinking of it before:

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

我相信您现在已经找到了另一个解决方案,但我仍然希望这有助于解决您将来可能遇到的任何问题.

I'm sure you've found another solution by now, but still, I hope this helps with any issues you might run into in the future.

这篇关于在 wordpress 中挂钩 the_content 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆