简体   繁体   English

显示产品品牌

[英]Showing product brand

For the last couple of hours I have been going over the same code over and over again. 在过去的几个小时中,我一遍又一遍地遍历相同的代码。 I am annoyed that it probably has a very simple fix. 我很生气,它可能有一个非常简单的修复程序。

Basically I am trying to show the brand of each product in a product slideshow in WP with WooCommerce Brands. 基本上,我试图通过WooCommerce Brands在WP产品幻灯片中显示每个产品的品牌。

Product slideshow is working but I am just unable to access the brand name of each product. 产品幻灯片正在运行,但是我无法访问每个产品的品牌名称。

I will just copy the code where I gather the details within the loop for products: 我将复制代码,在其中收集产品循环中的详细信息:

$product = get_sub_field('product_name');
$product_name = $product->post_title;
$product_link = get_post_permalink($product->ID);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->ID), 'full' );
$product = wc_get_product($product->ID ); 

The above is fine and allows me access to all the details apart from brand name. 上面的内容很好,让我可以访问除品牌名称之外的所有详细信息。 For brand name I have tried quite a few things including: 对于品牌名称,我尝试了很多事情,包括:

$brands = wp_get_object_terms(get_the_ID(), 'pwb-brand');
$product_brand = $brands->name;

$product-brand is not returning the brand name of the product in question... $ product-brand没有返回相关产品的品牌名称...

Any help would be greatly appreciated. 任何帮助将不胜感激。

I found this piece of code. 我找到了这段代码。 at this site http://www.techniquewebdesign.co.uk/show-woocommerce-brand-name/ 在此站点上http://www.techniquewebdesign.co.uk/show-woocommerce-brand-name/

<?php 
    $brands = wp_get_post_terms( $post->ID, 'product_brand', array("fields" => "all") );
    foreach( $brands as $brand ) {
        echo $brand->name; 
    }
?>

To show brand name in product details page you can use the following code in functions.php, this shows brand name before the product title. 要在产品详细信息页面中显示品牌名称,您可以在functions.php中使用以下代码,该代码在产品标题之前显示品牌名称。

remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);

add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5);

if ( ! function_exists( 'woocommerce_my_single_title' ) ) {
     function woocommerce_my_single_title() {

    global $product;
    $taxonomy = 'brand';
    $slug = 'brand'; //Or whatever the slug of "Brand" is
    $term = get_term_by('slug', $slug, $taxonomy);
    //Gets the ID of the Parent category

       $term_id = $term->term_id;
       //Get the Child terms
       $brand_terms = wp_get_post_terms( $product->id, $taxonomy, array("fields" => "all") );

           foreach ($brand_terms as $brand_item) {
           // Hunt out the child term that is a child of the parent
           if ( $brand_item->parent == $term_id ) {
               //Get the name of the brand

               $brand = $brand_item->name;
               break; //Assumes you've only assigned one Brand
           }
           }
 ?>  

    <h2 itemprop="name" class="product_title entry-title"><span><?php echo $brand ?></span></h2>
<?php
   }
}
?>

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

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