php
  • wordpress
  • woocommerce
  • categories
  • products
  • 2017-08-16 84 views 0 likes 
    0

    我在網上找到了一段代碼,該代碼當前列出了此WooCommerce網站上的所有類別。僅在WooCommerce中列出與當前產品相關的產品類別

    如何使它具體顯示與他們正在查看的產品相關的類別?

    這裏是我調整了代碼:

    <div id="ListCat">     
    <h3>Listed in the following categories<?php the_category(); ?></h3> 
    <?php 
    $taxonomy  = 'product_cat'; 
    $orderby  = 'name'; 
    $show_count = 0;  // 1 for yes, 0 for no 
    $pad_counts = 0;  // 1 for yes, 0 for no 
    $hierarchical = 0;  // 1 for yes, 0 for no 
    $title  = ''; 
    $empty  = 0; 
    
    $args = array(
        'taxonomy'  => $taxonomy, 
        'orderby'  => $orderby, 
        'show_count' => $show_count, 
        'pad_counts' => $pad_counts, 
        'hierarchical' => $hierarchical, 
        'title_li'  => $title, 
        'hide_empty' => $empty 
    ); 
    $all_categories = get_categories($args); 
    foreach ($all_categories as $cat) { 
    if($cat->category_parent == 0) { 
        $category_id = $cat->term_id;  
        echo ' <a href="'. get_term_link($cat->slug, 'product_cat') 
    .'">'. $cat->name .'</a>'; 
    
        $args2 = array(
         'taxonomy'  => $taxonomy, 
         'child_of'  => 0, 
         'parent'  => $category_id, 
         'orderby'  => $orderby, 
         'show_count' => $show_count, 
         'pad_counts' => $pad_counts, 
         'hierarchical' => $hierarchical, 
         'title_li'  => $title, 
         'hide_empty' => $empty 
        ); 
        $sub_cats = get_categories($args2); 
         if($sub_cats) { 
          foreach($sub_cats as $sub_category) { 
           echo '<br><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>'; 
          } 
         } 
        }  
    } 
    ?> 
    

    +0

    要知道如何識別您提供的代碼段中的產品,您很難知道所問問題的答案。 –

    回答

    1

    還有一種更簡單得多的方式:

    要顯示它作爲昏迷分離字符串(每個產品分類鏈接)

    // Display a coma separated string of the product categories for this product 
    echo wc_get_product_category_list(get_the_id()); 
    

    (或完全相似)

    // Display a coma separated string of the product categories for this product 
    echo get_the_term_list(get_the_id(), 'product_cat'); 
    

    要顯示它作爲格式化的HTML列表(針對每個產品類別的鏈接)

    // Display a html formatted list of the product categories for this product 
    echo '<ul>' . wc_get_product_category_list(get_the_id(), '</li><li>', '<li>', '</li>') . '</ul>'; 
    

    (或co mpletely類似)

    // Display a html formatted list of the product categories for this product 
    echo '<ul>' . get_the_term_list(get_the_id(), 'product_cat', '<li>', '</li><li>', '</li>') . '</ul>'; 
    

    說明:

    在Woocommerce 3有wc_get_product_category_list()專用功能列出從產品ID的產品類別,有一些可用的就可以了,就像你可以看到參數在其源代碼中:

    /** 
    * Returns the product categories in a list. 
    * 
    * @param int $product_id 
    * @param string $sep (default: ', '). 
    * @param string $before (default: ''). 
    * @param string $after (default: ''). 
    * @return string 
    */ 
    function wc_get_product_category_list($product_id, $sep = ', ', $before = '', $after = '') { 
        return get_the_term_list($product_id, 'product_cat', $before, $sep, $after); 
    } 
    

    正如你在這個源代碼中看到的那樣,它是WordPress get_the_term_list()函數的一個別名,'product_cat'作爲$taxonomy的參數,你也可以使用它。

    +0

    謝謝!我傾向於將非常簡單的事情複雜化!代碼的第一部分已經完成了! –

    相關問題