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

WordPress插件开发:仅在文章含.test类时加载资源

WordPress插件:基于ACF嵌套字段检测加载资源

解决方案代码

首先添加一个递归辅助函数,用来遍历ACF字段内容并检测是否包含.test类:

/**
 * 递归检查ACF字段内容中是否包含指定CSS类
 *
 * @param mixed $acf_field_value ACF字段值(数组/字符串)
 * @param string $target_class 目标类名
 * @return bool
 */
function has_target_css_class( $acf_field_value, $target_class ) {
    // 检查字符串内容中的class属性
    if ( is_string( $acf_field_value ) ) {
        return preg_match( '/class="[^"]*\b' . preg_quote( $target_class, '/' ) . '\b[^"]*"/i', $acf_field_value ) === 1;
    }

    // 递归遍历数组类型的字段(灵活内容、分组字段等)
    if ( is_array( $acf_field_value ) ) {
        foreach ( $acf_field_value as $value ) {
            if ( has_target_css_class( $value, $target_class ) ) {
                return true;
            }
        }
    }

    return false;
}

然后在资源加载钩子中,同时检查默认文章内容和ACF嵌套字段:

add_action( 'wp_enqueue_scripts', 'enqueue_assets_based_on_test_class' );
function enqueue_assets_based_on_test_class() {
    // 仅在单篇文章/页面执行检测
    if ( ! is_singular() ) {
        return;
    }

    global $post;
    $has_test_class = false;

    // 1. 检查默认文章内容
    if ( preg_match( '/class="[^"]*\btest\b[^"]*"/i', $post->post_content ) ) {
        $has_test_class = true;
    }

    // 2. 检查ACF嵌套字段:替换为你的实际字段键
    $acf_group = get_field( 'your_group_field_key', $post->ID );
    if ( $acf_group ) {
        $flexible_blocks = $acf_group['your_flexible_content_key'];
        if ( $flexible_blocks ) {
            foreach ( $flexible_blocks as $block ) {
                // 检查column_1和column_2富文本字段
                if ( isset( $block['column_1'] ) && has_target_css_class( $block['column_1'], 'test' ) ) {
                    $has_test_class = true;
                    break;
                }
                if ( isset( $block['column_2'] ) && has_target_css_class( $block['column_2'], 'test' ) ) {
                    $has_test_class = true;
                    break;
                }
            }
        }
    }

    // 检测到目标类则加载资源
    if ( $has_test_class ) {
        // 加载样式表
        wp_enqueue_style(
            'test-custom-style',
            plugin_dir_url( __FILE__ ) . 'css/test-style.css',
            array(),
            '1.0.0'
        );

        // 加载第一个JS文件(可按需调整依赖)
        wp_enqueue_script(
            'test-first-script',
            plugin_dir_url( __FILE__ ) . 'js/test-first.js',
            array( 'jquery' ),
            '1.0.0',
            true
        );

        // 加载第二个JS文件(可按需调整依赖)
        wp_enqueue_script(
            'test-second-script',
            plugin_dir_url( __FILE__ ) . 'js/test-second.js',
            array( 'test-first-script' ),
            '1.0.0',
            true
        );
    }
}

关键说明

  • 字段替换:把代码中的your_group_field_key和your_flexible_content_key替换为你ACF字段的实际键(不是后台显示的名称,可在ACF字段编辑页查看)
  • 正则匹配:使用\b单词边界确保只匹配独立的.test类,避免误匹配testimonial这类包含"test"的字符串
  • 递归检测:辅助函数支持任意层级的ACF嵌套字段,即使你的灵活内容有更多子字段也能覆盖
  • 性能优化:仅在单页面/post执行检测,避免全局运行带来的性能损耗

内容的提问来源于stack exchange,提问作者François

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 01:55:28