简体   繁体   English

在单个产品页面简码上显示产品类别 AFC 字段

[英]Display product category AFC field on Single product page shortcode

I want to display advanced custom fields from my Product categorie on the product page that it is in.我想在我的产品类别所在的产品页面上显示高级自定义字段。

so lets say I am on Home > category1 > category2 > Product所以假设我在主页 > 类别 1 > 类别 2 > 产品

Category 2 is always a brand so I made a custom field where the value is "YES" if the product is a brand an "NO" if it isn't a brand.类别 2 始终是品牌,因此我创建了一个自定义字段,如果产品是品牌,则值为“是”,如果不是品牌,则值为“否”。

This displays the category AFC but not the last one so not category2 but category1 code这显示类别 AFC 但不是最后一个,所以不是 category2 而是 category1 code

<?php
function get_current_product_category1(){

        global $post;

       $terms = get_the_terms( $post->ID, 'product_cat' );

        $nterms = get_the_terms( $post->ID, 'product_tag'  );

        foreach ($terms  as $term  ) {                    

            $product_cat_id = $term->term_id;              

            // $product_cat_name = $term->name;      
            
            $product_cat_name = get_field('merk_header_afbeelding', $term);
            

            break;

        }

       return $product_cat_name['url'];


}
add_shortcode ('categorie_acf2', 'get_current_product_category1');
?>

So I made a custom field that makes it a category2 or category1 and I filter on that so I only get back the Category2 items所以我创建了一个自定义字段,使其成为 category2 或 category1 并对其进行过滤,因此我只取回 Category2 项目

<?php
        function get_current_product_category5(){
    
            global $post;
    
           $terms = get_the_terms( $post->ID, 'product_cat' );
    
            $nterms = get_the_terms( $post->ID, 'product_tag'  );
    
            foreach ($terms  as $term  ) {                    
    
                $product_cat_id = $term->term_id;              
                
                $product_cat_name = get_field('is_is_deze_categorie_een_merk', $term);
    
              
                    //check if product AFC is ja
                    if($product_cat_name == "ja")
                    {
                        $product_cat_name2 = get_field('merk_header_afbeelding', $term);
                    }
    
            }
    
           return $product_cat_name2['url'];
    
    
    }
    add_shortcode ('categorie_acf3', 'get_current_product_category5');
?>

You have a break inside the for loop.您在 for 循环中有一个中断。 Try without the break.尝试不间断。

EDIT: Adding the code snippet to print the link to the category with "brand" custom field set to "Yes".编辑:添加代码片段以打印指向“品牌”自定义字段设置为“是”的类别的链接。

This will print only the first category with brand "Yes".这将仅打印品牌为“Yes”的第一个类别。 If there are more of them, then you should concatenate all of them into one string.如果有更多,那么你应该将它们全部连接成一个字符串。

function get_current_product_category5(){

    global $post;

    $terms = get_the_terms( $post->ID, 'product_cat' );

    foreach ($terms  as $term  ) {

        $is_brand = get_field('is_brand', $term);
        if($is_brand == "Yes")
        {
            return '<a href="' . esc_url( get_term_link( $term ) ) . '">'.$term->name.'</a>';
        }
    }
}
add_shortcode ('categorie_acf3', 'get_current_product_category5');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM