Php 展示产品品牌

Php 展示产品品牌,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在过去的几个小时里,我一直在一遍又一遍地重复同样的代码。我很恼火,它可能有一个非常简单的修复 基本上,我试图在WP中与WooCommerce Brands的产品幻灯片中展示每个产品的品牌 产品幻灯片正在运行,但我无法访问每个产品的品牌名称 我将只复制代码,在其中收集产品循环中的详细信息: $product = get_sub_field('product_name'); $product_name = $product->post_title; $product_link = get_po

在过去的几个小时里,我一直在一遍又一遍地重复同样的代码。我很恼火,它可能有一个非常简单的修复

基本上,我试图在WP中与WooCommerce Brands的产品幻灯片中展示每个产品的品牌

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

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

$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未返回相关产品的品牌名称


任何帮助都将不胜感激。

我找到了这段代码。在这个网站上


我找到了这段代码。在这个网站上


要在产品详细信息页面中显示品牌名称,可以在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
   }
}
?>
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
添加行动(“woocommerce单品摘要”,“woocommerce单品标题”,5);
如果(!function_存在('woocommerce_my_single_title')){
功能名称(我的单标题){
全球$产品;
$taxonomy=‘品牌’;
$slug='brand';//或者不管“brand”的slug是什么
$term=get_term_by('slug',$slug,$taxonomy);
//获取父类别的ID
$term\u id=$term->term\u id;
//得到孩子们的条件
$brand_terms=wp_get_post_terms($product->id,$taxonomy,array(“fields”=>“all”));
foreach($brand_术语作为$brand_项目){
//找出父项的子项
如果($brand\u item->parent==$term\u id){
//获取品牌名称
$brand=$brand\u项目->名称;
break;//假设您只指定了一个品牌
}
}
?>  

要在产品详细信息页面中显示品牌名称,可以在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
   }
}
?>
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
添加行动(“woocommerce单品摘要”,“woocommerce单品标题”,5);
如果(!function_存在('woocommerce_my_single_title')){
功能名称(我的单标题){
全球$产品;
$taxonomy=‘品牌’;
$slug='brand';//或者不管“brand”的slug是什么
$term=get_term_by('slug',$slug,$taxonomy);
//获取父类别的ID
$term\u id=$term->term\u id;
//得到孩子们的条件
$brand_terms=wp_get_post_terms($product->id,$taxonomy,array(“fields”=>“all”));
foreach($brand_术语作为$brand_项目){
//找出父项的子项
如果($brand\u item->parent==$term\u id){
//获取品牌名称
$brand=$brand\u项目->名称;
break;//假设您只指定了一个品牌
}
}
?>