首页 > 解决方案 > 如何使用 acf 将自定义字段添加到 woocommerce 商店页面

问题描述

为了将高级自定义字段集成到 woocommerce 商店页面,我遇到了一些困难。我的代码是一个转发器字段,我需要将它添加到商店页面的顶部,但这不起作用。我已经尝试通过添加页面 id 并且没有任何反应。任何帮助或建议将不胜感激。

我在我的子主题中创建了一个 woocommerce 文件夹,我正在尝试将此代码添加到 archive-product.php 模板。

<div class="container accueil">

        <h2 class="titre-section">Tous nos saveurs à un seul clic !</h2>

        <div class="row products-categories">

            <?php if( have_rows('categories_menu') ): ?>



                <?php while( have_rows('categories_menu') ): the_row(); 

                    // vars
                    $titre = get_sub_field('titre_categorie');
                    $image = get_sub_field('image_categorie');
                    ?>
                    <a class="link-cat" href="/carte/">
                    <div class="col-md-4 product-cat" style="background-image: url(<?php echo $image['url']; ?>);">
                        <h2 class="cat-title"><?php echo $titre; ?></h2>    
                    </div>
                    </a>
                <?php endwhile; ?>

            <?php endif; ?>

        </div>
    </div>

我没有错误消息。

标签: phpwordpresswoocommerceadvanced-custom-fields

解决方案


您需要获取 Woo-commerce 商店页面 ID,然后将这些 ID 传递给每个 get_field & rows

<?php if(is_shop()){ ?>
<div class="container accueil">

        <h2 class="titre-section">Tous nos saveurs à un seul clic !</h2>

        <div class="row products-categories">

            <?php $post_id = get_option( 'woocommerce_shop_page_id' ); ?>

            <?php if( have_rows('categories_menu', $post_id) ): ?>

                <?php while( have_rows('categories_menu', $post_id) ): the_row(); 

                    $titre = get_sub_field('titre_categorie', $post_id);
                    $image = get_sub_field('image_categorie', $post_id);
                    ?>
                    <a class="link-cat" href="/carte/">
                    <div class="col-md-4 product-cat" style="background-image: url(<?php echo $image['url']; ?>);">
                        <h2 class="cat-title"><?php echo $titre; ?></h2>    
                    </div>
                    </a>
                <?php endwhile; ?>

            <?php endif; ?>

        </div>
    </div>
<?php } ?>

推荐阅读