Slick Slider无法识别自定义Brands分类问题求助
问题描述
- 需求:实现Slick Slider从自定义分类「Brands」调取广告图及跳转链接,展示逻辑为优先显示当前页面上传的广告图,无则调用页面关联品牌的广告内容,创建品牌时已为其配置广告图与链接字段。
- 异常:当前页面广告图调用功能正常,但页面关联品牌时无法识别「Brands」分类,无法加载对应品牌的广告内容。
现有代码
<div class="top-ad td-container-wrap"> <div class="td-container"> <div class="td-crumb-container"> <div class="slick-slider"> <?php if( have_rows('top_advert_banner') ): ?> <?php // loop through the rows of data while ( have_rows('top_advert_banner') ) : the_row(); $image = get_sub_field('advert_image'); $size = 'full'; $link = get_sub_field('advert_link'); ?> <div class="ad-slide"> <?php if ($link) : ?><a href="<?php echo $link; ?>" target="_blank"><?php endif; ?> <?php echo wp_get_attachment_image( $image, $size ); ?> <?php if ($link) : ?></a><?php endif; ?> </div> <?php endwhile; ?> <?php else : ?> <?php $tax_term = wp_get_post_terms($post->ID, 'brands', array("fields" => "ids")); $term_id = ($tax_term[0]); if( have_rows('top_advert_banner', 'brands_' . $term_id) ): ?> <?php while( have_rows('top_advert_banner', 'brands_' . $term_id) ): the_row(); $image = get_sub_field('advert_image'); $size = 'full'; $link = get_sub_field('advert_link'); ?> <div class="ad-slide"> <?php if ($link) : ?><a href="<?php echo $link; ?>" target="_blank"><?php endif; ?> <?php echo wp_get_attachment_image( $image, $size ); ?> <?php if ($link) : ?></a><?php endif; ?> </div> <?php endwhile; ?> <?php else : ?> <?php if( have_rows('top_advert_banner', 28) ): ?> <?php while ( have_rows('top_advert_banner', 28) ) : the_row(); $image = get_sub_field('advert_image'); $size = 'top-ad'; $link = get_sub_field('advert_link'); ?> <div class="ad-slide"> <?php if ($link) : ?><a href="<?php echo $link; ?>" target="_blank"><?php endif; ?> <?php echo wp_get_attachment_image( $image, $size ); ?> <?php if ($link) : ?></a><?php endif; ?> </div> <?php endwhile; ?> <?php endif; ?> <?php endif; ?> <?php endif; ?> </div> </div> </div>
排查与修复方案
1. 验证自定义分类配置
确认「Brands」自定义分类的slug为brands(代码中依赖该slug),且注册时关联了目标页面类型;若使用Gutenberg编辑器,需确保分类注册时show_in_rest参数设为true。
2. 修复分类术语获取逻辑
原代码未处理无关联品牌的情况,且依赖全局$post变量存在风险,替换为以下代码:
$tax_terms = wp_get_post_terms(get_the_ID(), 'brands', array('fields' => 'ids')); $term_id = !empty($tax_terms) ? $tax_terms[0] : 0;
- 用
get_the_ID()替代$post->ID,避免全局变量未初始化的问题 - 增加空值判断,防止无关联品牌时触发无效调用
3. 确认ACF字段关联
检查ACF字段组top_advert_banner的位置规则,确保其绑定到「Brands」分类(位置规则选择「分类」→「等于」→「Brands」)。
4. 调试验证
在获取$term_id后添加调试代码,确认是否正确获取品牌ID及字段数据:
var_dump($term_id); // 输出品牌ID,确认是否获取成功 var_dump(get_field('top_advert_banner', 'brands_' . $term_id)); // 检查字段是否存在
调试完成后删除该代码。
修复后的核心代码片段
<?php else : ?> <?php $tax_terms = wp_get_post_terms(get_the_ID(), 'brands', array('fields' => 'ids')); $term_id = !empty($tax_terms) ? $tax_terms[0] : 0; if( $term_id && have_rows('top_advert_banner', 'brands_' . $term_id) ): ?> <?php while( have_rows('top_advert_banner', 'brands_' . $term_id) ): the_row(); $image = get_sub_field('advert_image'); $size = 'full'; $link = get_sub_field('advert_link'); ?> <div class="ad-slide"> <?php if ($link) : ?><a href="<?php echo esc_url($link); ?>" target="_blank"><?php endif; ?> <?php echo wp_get_attachment_image( $image, $size ); ?> <?php if ($link) : ?></a><?php endif; ?> </div> <?php endwhile; ?> <?php else : ?> <?php if( have_rows('top_advert_banner', 28) ): ?> <?php while ( have_rows('top_advert_banner', 28) ) : the_row(); $image = get_sub_field('advert_image'); $size = 'top-ad'; $link = get_sub_field('advert_link'); ?> <div class="ad-slide"> <?php if ($link) : ?><a href="<?php echo esc_url($link); ?>" target="_blank"><?php endif; ?> <?php echo wp_get_attachment_image( $image, $size ); ?> <?php if ($link) : ?></a><?php endif; ?> </div> <?php endwhile; ?> <?php endif; ?> <?php endif; ?> <?php endif; ?>
- 增加
$term_id非空判断,避免无效调用 - 用
esc_url()处理跳转链接,提升站点安全性
内容的提问来源于stack exchange,提问作者Koden
相关产品推荐
相关产品推荐

