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

如何用PHP替换自定义文章类型单页的the_content()值适配Passster插件?

解决Passster插件保护ACF自定义模板的问题

当然可以通过自定义PHP代码实现,这里提供两种实用方案:

方法1:通过过滤器覆盖the_content()的值

利用WordPress的the_content过滤器,把你的ACF字段内容和自定义HTML整合到the_content()的返回值中,让Passster的保护机制触发。

示例代码(放到主题的functions.php或自定义插件里):

add_filter('the_content', 'custom_acf_content_to_the_content', 10);
function custom_acf_content_to_the_content($content) {
    // 仅针对目标自定义文章类型生效
    if (is_singular('your_custom_post_type')) {
        $acf_content = '';
        // 拼接ACF字段(替换为你的实际字段名)
        if (get_field('your_acf_field_1')) {
            $acf_content .= '<div class="acf-field-1">' . get_field('your_acf_field_1') . '</div>';
        }
        if (get_field('your_acf_field_2')) {
            $acf_content .= '<div class="acf-field-2">' . get_field('your_acf_field_2') . '</div>';
        }
        // 追加自定义HTML
        $custom_html = '<div class="custom-section">你的自定义HTML内容</div>';
        
        // 替换原内容为拼接后的内容
        $content = $acf_content . $custom_html;
    }
    return $content;
}

之后在模板里调用<?php the_content(); ?>(哪怕放在隐藏容器里),Passster就会对拼接后的内容生效。

方法2:直接调用Passster的保护函数(更灵活)

不依赖the_content(),直接在模板里用Passster提供的passster_protect()函数包裹你的内容,针对性更强。

示例代码(放到自定义文章类型模板里):

// 组装需要保护的内容
$protected_content = '';
// 拼接ACF字段
if (get_field('your_acf_field_1')) {
    $protected_content .= '<div class="acf-field-1">' . get_field('your_acf_field_1') . '</div>';
}
if (get_field('your_acf_field_2')) {
    $protected_content .= '<div class="acf-field-2">' . get_field('your_acf_field_2') . '</div>';
}
// 追加自定义HTML
$protected_content .= '<div class="custom-section">你的自定义HTML内容</div>';

// 调用Passster保护函数,支持自定义表单设置
echo passster_protect($protected_content, array(
    'form_title' => '请输入密码查看内容',
    'submit_label' => '提交'
));

如果用了Passster的全局密码设置,上面的数组参数可以省略,直接写echo passster_protect($protected_content);即可。

注意事项

  • 替换your_custom_post_type为你的自定义文章类型slug
  • 替换your_acf_field_1、your_acf_field_2为实际的ACF字段名称
  • 方法1需要确保模板里存在the_content()调用,哪怕是隐藏状态

内容的提问来源于stack exchange,提问作者Mathew Smith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 03:28:17