You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中Smash Balloon插件加载更多后触发PHP脚本问题

解决Smash Balloon FB Feed加载更多后脚本不生效的问题

核心原因

初始页面内容通过the_content钩子处理,但点击「加载更多」是AJAX异步请求,插件直接返回渲染好的帖子HTML,不会走the_content过滤,所以新加载的内容没被你的脚本修改。

解决方案一:用插件专属后端钩子(推荐)

替换原来的the_content钩子,改用Smash Balloon提供的cff_post_html钩子——这个钩子会处理所有帖子的HTML,不管是初始加载还是AJAX加载的,更精准可靠。

替换后的代码:

<?php
function modify_fb_feed_post_html($post_html, $post, $options) {
    global $wp_post;
    // 仅在ID为63的页面生效
    if( $wp_post->ID == 63 ){
        // 替换链接为自定义按钮,正则规则和你原来的一致
        $post_html = preg_replace ('{<a.*?href="([^"]*)".*?>(.*?)</a>}i', '<a target="_blank" href="$1" class="btn btn-danger btn-sm rounded-0 transition-3d-hover btn-width display-buy-now" style="width:250px !important">Buy Now</a>', $post_html);
    }
    return $post_html;
}
add_filter('cff_post_html', 'modify_fb_feed_post_html', 10, 3);
?>
  • 这里优先级设为10即可,因为cff_post_html是插件专属钩子,很少有其他冲突,不用设到995。

解决方案二:前端JS监听加载完成事件

如果后端钩子不生效,可通过前端JS监听插件的「加载完成」事件,对新加载的内容重新执行替换逻辑:

  1. 在主题functions.php中添加JS加载代码:
<?php
function add_custom_fb_feed_js() {
    // 仅在ID为63的页面加载脚本
    if( is_page(63) ){
        wp_enqueue_script('custom-fb-feed-js', get_stylesheet_directory_uri() . '/js/custom-fb-feed.js', array('jquery'), '1.0', true);
    }
}
add_action('wp_enqueue_scripts', 'add_custom_fb_feed_js');
?>
  1. 在主题目录下创建js/custom-fb-feed.js文件,写入以下代码:
jQuery(document).on('cff_loaded', function() {
    // 给未处理过的帖子添加标记,避免重复替换
    jQuery('.cff-item:not(.processed)').addClass('processed').find('a').each(function() {
        const linkHref = jQuery(this).attr('href');
        // 替换原有链接为自定义按钮
        jQuery(this).replaceWith(`<a target="_blank" href="${linkHref}" class="btn btn-danger btn-sm rounded-0 transition-3d-hover btn-width display-buy-now" style="width:250px !important">Buy Now</a>`);
    });
});

关于优先级995的说明

你之前设995是因为the_content钩子有大量主题、插件的过滤逻辑,高优先级(数字越大执行越晚)能确保你的替换是最后一步,覆盖之前的修改。但改用cff_post_html钩子后,因为是插件专属,无需这么高的优先级。

内容的提问来源于stack exchange,提问作者Mike Muller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 15:03:21