【问题标题】:Showing product brand展示产品品牌
【发布时间】:2018-03-11 13:33:04
【问题描述】:

在过去的几个小时里,我一遍又一遍地检查相同的代码。我很生气,它可能有一个非常简单的修复方法。

基本上,我试图在带有 WooCommerce Brands 的 WP 产品幻灯片中展示每种产品的品牌。

产品幻灯片正在运行,但我无法访问每个产品的品牌名称。

我将只复制我在产品循环中收集详细信息的代码:

$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 ); 

以上内容很好,让我可以访问除品牌名称之外的所有详细信息。对于品牌名称,我尝试了很多方法,包括:

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

$product-brand 未返回相关产品的品牌名称...

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    要在产品详细信息页面显示品牌名称,您可以在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
       }
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      我找到了这段代码。在本站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; 
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-10
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        相关资源
        最近更新 更多