php
  • wordpress
  • woocommerce
  • categories
  • products
  • 2017-08-16 83 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

    谢谢!我倾向于将非常简单的事情复杂化!代码的第一部分已经完成了! –

    相关问题