【问题标题】:Show Description of Woocommerce Subcategory on Parent Category在父类别上显示 Woocommerce 子类别的描述
【发布时间】:2021-08-16 20:12:58
【问题描述】:

我正在处理一个场景,其中有多个父类别,每个父类别都有多个子类别。

我已经将父类别设置为显示子类别而不是产品。

这是我当前的层次结构:

父类 1

  • 子类别 1
  • 子类别 2
  • 子类别 3

父类别 2

  • 子类别 4
  • 子类别 5
  • 子类别 6

一切正常,但是,我想做一件额外的事情,在所有子类别显示在父类别下时显示它们的描述。

我知道查看父类别时默认显示子类别的标题,但是我找不到覆盖默认循环以在父类别中显示子类别描述的方法。

任何帮助将不胜感激!

谢谢!

更新:

我已成功在父类别页面上显示子类别名称和描述,代码如下:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $output = '<ul class="subcategories-list">';

    // Loop through product subcategories WP_Term Objects
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );

        


        echo '<h3'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></h3>';

        echo '<p class="'. $term->slug .'">'. $term->description .'</a></p>';
        
    
    }
    echo '</ul';
    
    
}

但是,我在获取子类别的图像时遇到了麻烦。所以这方面的任何帮助都会非常有用。

【问题讨论】:

  • 请添加一些代码。你尝试了什么,你得到了什么。让社区更好地理解您的问题
  • @JoyKumarBera 更新了代码

标签: wordpress woocommerce


【解决方案1】:

像这样修改你的 foreach 循环

foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );
        $thumb_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
        $thumb_src = wp_get_attachment_image_src( $thumb_id );

        echo '<h3'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></h3>';

        echo '<p class="'. $term->slug .'">'. $term->description .'</a></p>';

        echo '<img src="'.$thumb_src[0].'">';
}

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多