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

使用Advanced Custom Fields添加自定义URL后按钮跳转异常求助

问题:ACF自定义产品链接不生效,按钮仍跳转至产品默认页面

网站通过ACF从文章中拉取产品描述与链接,默认点击「Go to Product」按钮会跳转至对应产品页面。现需添加自定义URL功能,让按钮可跳转至指定外部/自定义地址,而非产品默认页面。

已在ACF的产品选择字段上方添加了product_link字段,但修改代码后测试发现按钮仍跳转至产品默认页面,自定义URL未生效。


原始代码

<?php if ($featured_product_enable): ?>
    
    <?php if (have_rows('featured_product')): ?>
        <?php while (have_rows('featured_product')):
            the_row();
            // $blurb = get_sub_field( 'blurb' );
            $product = get_sub_field('product');
            $product_featured_excerpt = get_field('featured_excerpt', $product->ID);
        ?>

            <?php if ($product): ?>
                <div class="feature-box <?php echo $feature_box_class; ?> margin-bottom-2">
                    <h4>Featured Product</h4>
                    <p><strong><?php echo $product->post_title; ?></strong><?php echo $product_featured_excerpt ? ' - ' . $product_featured_excerpt : '' ?></p>
                    <p><a href="<?php the_permalink($product->ID); ?>" class="<?php echo $button_class; ?>">Go to Product</a></p>
                </div>
            <?php endif; ?>

        <?php endwhile; ?>
    <?php endif; ?>

<?php endif; ?>

修改后的代码

<?php if ($featured_product_enable): ?>
    
    <?php if (have_rows('featured_product')): ?>
        <?php while (have_rows('featured_product')):
            the_row();
            // $blurb = get_sub_field( 'blurb' );
            $product = get_sub_field('product');
            $product_featured_excerpt = get_field('featured_excerpt', $product->ID);
            $product_link = get_field('product_link');
        ?>

            <?php if ($product): ?>
                <div class="feature-box <?php echo $feature_box_class; ?> margin-bottom-2">
                    <h4>Featured Product</h4>
                    <p><strong><?php echo $product->post_title; ?></strong><?php echo $product_featured_excerpt ? ' - ' . $product_featured_excerpt : '' ?></p>
                    <p><a href="<?php echo esc_url($product_link); ?>" class="<?php echo $button_class; ?>">Go to Product</a></p>
                </div>
            <?php endif; ?>

        <?php endwhile; ?>
    <?php endif; ?>

<?php endif; ?>

解决方案

核心问题:字段获取方式错误

product_link是添加在featured_product重复字段组内的子字段,当前代码用get_field('product_link')获取,该函数默认读取当前文章的顶级字段,而非重复组内的子字段,导致无法正确获取自定义URL。

修正后的代码

<?php if ($featured_product_enable): ?>
    
    <?php if (have_rows('featured_product')): ?>
        <?php while (have_rows('featured_product')):
            the_row();
            $product = get_sub_field('product');
            $product_featured_excerpt = get_field('featured_excerpt', $product->ID);
            // 改用get_sub_field获取重复组内的子字段
            $product_link = get_sub_field('product_link');
            
            // 逻辑优化:如果自定义链接存在则使用,否则 fallback 到产品默认链接
            $target_url = !empty($product_link) ? esc_url($product_link) : get_permalink($product->ID);
        ?>

            <?php if ($product): ?>
                <div class="feature-box <?php echo $feature_box_class; ?> margin-bottom-2">
                    <h4>Featured Product</h4>
                    <p><strong><?php echo $product->post_title; ?></strong><?php echo $product_featured_excerpt ? ' - ' . $product_featured_excerpt : '' ?></p>
                    <p><a href="<?php echo $target_url; ?>" class="<?php echo $button_class; ?>">Go to Product</a></p>
                </div>
            <?php endif; ?>

        <?php endwhile; ?>
    <?php endif; ?>

<?php endif; ?>

额外检查点

  1. 确认ACF字段配置:product_link字段确实属于featured_product重复字段组,而非当前文章的顶级字段
  2. 后台验证:在对应页面的ACF重复组中,已正确填写自定义URL内容
  3. 缓存清理:清除网站缓存(包括页面缓存、ACF缓存),避免旧代码/数据残留
  4. 字段名称校验:确保代码中使用的字段名product_link与ACF后台配置的字段键完全一致(大小写敏感)

内容的提问来源于stack exchange,提问作者David V.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 13:41:01