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

ACF Flexible Content的have_rows()函数在WooCommerce分类页失效

WooCommerce分类法页面ACF Flexible Content have_rows()返回false的解决方法

在WooCommerce的分类/标签等分类法页面中使用ACF Flexible Content字段时,have_rows()函数始终返回false,但通过var_dump()或print_r()查看字段数据时,能看到存有有效内容的数组,且已尝试ACF官方文档提供的两种调用方式:

第一种代码:

<?php
$current_category = get_queried_object();

if( have_rows('contenu_flexible', $current_category) ):

    // Loop through rows.
    while ( have_rows('contenu_flexible', $current_category) ) : the_row();

第二种代码:

<?php
$current_category = get_queried_object();

if( have_rows('contenu_flexible', $current_category->taxonomy.'_'.$current_category->term_id) ):
    // Loop through rows.
    while ( have_rows('contenu_flexible', $current_category->taxonomy.'_'.$current_category->term_id) ) : the_row();

两种写法均无法正常触发循环,可尝试以下解决方案:

  • 直接获取字段数组手动遍历
    既然字段数据已存在,可跳过have_rows(),直接用get_field()拿到Flexible Content的数组后手动循环处理:
<?php
$current_category = get_queried_object();
$flexible_content = get_field('contenu_flexible', $current_category);

if ($flexible_content) {
    foreach ($flexible_content as $row) {
        // 根据布局类型匹配处理
        switch ($row['acf_fc_layout']) {
            case 'layout_name_1':
                // 输出对应布局内容,示例:echo $row['custom_field'];
                break;
            case 'layout_name_2':
                // 处理第二种布局
                break;
            // 其他布局依次添加
        }
    }
}
  • 检查ACF字段的位置规则
    确认Flexible Content字段的位置设置,是否正确关联到目标分类法(比如产品分类product_cat、产品标签product_tag),避免字段被绑定到错误的分类类型或自定义分类法上。

  • 验证当前查询对象的正确性
    在代码开头添加var_dump($current_category);,确认返回的对象是当前页面对应的WooCommerce分类/标签,检查taxonomy和term_id属性是否准确,排除页面上下文导致的对象错误。

  • 重置ACF缓存
    ACF的缓存可能导致字段读取异常,可在调用字段前添加缓存重置代码:

<?php
acf_reset_vars();
$current_category = get_queried_object();

内容的提问来源于stack exchange,提问作者Quentin Sauvaire

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 11:07:38