【问题标题】:Product category linked terms list related to current product in WooCommerceWooCommerce 中与当前产品相关的产品类别链接术语列表
【发布时间】:2018-01-24 21:40:02
【问题描述】:

我在网上找到了一段代码,目前列出了这个 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>';
            }
        }
    }       
}
?>

【问题讨论】:

  • 知道如何在您提供的 sn-p 中识别产品,很难知道您所提问题的答案。

标签: php wordpress woocommerce categories product


【解决方案1】:

如果您需要它们而没有类别链接,请使用此代码

foreach((get_the_terms( $post->ID, 'product_cat' )) as $category) {
    echo $category->name . '</br>';
}

【讨论】:

    【解决方案2】:

    还有一种更简单的方法:

    将其显示为逗号分隔的字符串 (每个产品类别链接)

    // 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>';
    

    (或完全相似)

    // 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 参数,您也可以使用它来代替。

    【讨论】:

    • 谢谢!我倾向于将非常简单的事情过度复杂化!第一个 sn-p 代码就成功了!
    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2019-08-05
    • 2017-03-27
    • 1970-01-01
    • 2018-11-09
    • 2013-08-11
    • 2017-03-12
    相关资源
    最近更新 更多