当前位置: 首页 > 知识库问答 >
问题:

Woocommerce产品的ACF关系字段(按类别)

侯和惬
2023-03-14

我使用ACF关系字段来显示每个产品的相关博客文章。目前它的工作,但我必须检查每个产品手动显示一个相关的职位上。

$posts = get_posts(array(
        'meta_query' => array(
            array(
                'key' => 'products', // name of custom field
                'value' => '"' . $product->get_id() . '"',
                'compare' => 'LIKE'
            )
        )
    ));

问题:我如何添加产品类别,这样我就不需要手动添加200个产品,但只有一个产品类别与这篇文章相关,并将在单一视图下显示这篇文章。

Possible solution?

是否可以将其与分类ACF字段集成(如果post具有相关的分类产品,它将显示在该类别的产品下)?可惜想不出办法

共有1个答案

赵智
2023-03-14
<?php
global $post;

//Related Product Section Start

$term_list = wp_get_post_terms($post->ID, 'your_taxonomy', array("fields" => "ids", "parent" => 0 ));
$term_name = wp_get_post_terms($post->ID, 'your_taxonomy', array("fields" => "names", "parent" => 0));

if ( $term_list ) { 
    $data_related = new WP_Query 
        ( 
            array (
                'post_type' => 'your_post_type',
                'posts_per_page' => -1,
                'post__not_in' => array( $post->ID ),
                'post_status' => 'publish',
                'tax_query' => array(
                    array (
                        'taxonomy' => 'your_taxonomy',
                        'field' => 'term_id',
                        'terms'    => $term_list[0],
                    ),
                ),
            ) 
        );

    if ( $data_related->have_posts() ) { ?>
        <div class="product-part">
            <div class="container">
                <h2><?php echo $term_name[0]; ?></h2>
                <?php  while ( $data_related->have_posts() ) {
                    $data_related->the_post();  
                    echo '<div class="col-12  col-md-6 col-lg-4 col-xl-4">' .
                            '<h6>' . get_the_title( get_the_ID() ) . '</h6>' .
                    '</div>';
                } ?>
             </div>        
        </div>
    <?php } wp_reset_query(); wp_reset_postdata();
} 

//Related Product Section End ?>

尝试此代码,将此代码放在单个产品页中

 类似资料:
  • 我成功地在后端的产品变化中的WooCommerce中创建了一个ACF自定义字段。该字段在每个变化中正确显示。 但是在编辑变体中的此字段或其他字段后,我无法再保存整个变体选项卡。加载/保存指示器圆圈继续无限旋转。并且定制字段未显示在前端的单个产品变体中。 我所做的是将以下代码添加到我的: 任何帮助都将不胜感激。

  • 我需要能够根据来自ACF字段的值筛选WooCommerce产品,其中包含一系列复选框,例如Apple、Bananas、Grapes等。理想情况下,我希望能够通过URL查询(例如https://example.com/shop/?fruit=Apples)来实现这一点 我一直在尝试这些方法,但没有成功:

  • “我的店铺”中的每个产品都有一个ACF图像字段,该字段显示与产品(活动)关联的t恤图像。 现在我需要客户在结账时看到这个图像。我决定创建一个短代码,并在结账页面的某个地方添加它。 我不知道如何从购物车中的项目获得ACF图像字段? 到目前为止,我试图使用这段代码进入。任何想法? 注意:我们运行一个设置,即购物车中只能有一个产品。

  • 我为woocommerce产品“coffee_type”创建了自定义分类法。我为此分类法创建了一个ACF图像字段“coffee\u type\u image”。 我想在产品页面上显示带有链接的图像,而不是分类法的名称。我在这里读了大多数关于在分类法上显示acf图像的文章,但每一篇都是关于使用归档分类法页面而不是产品页面。我正在编辑single-product.php(确切地content-sing

  • 在我的帖子页面(常规帖子类型)中,我设置了一个ACF关系字段。在这个里面,我可以选择公司名称,这些名称都在directory_listings的职位类型下。 现在,我在directory listings页面上有以下代码,因此使用simply get_字段不起作用,因为这些值不在该页面上,而是在POST类型的其他位置。 因此不确定如何获取信息。 目录_LISTINGS post type下某个页面