【问题标题】:Show subcategory instead of parent category显示子类别而不是父类别
【发布时间】:2016-07-21 03:59:22
【问题描述】:

我使用下面的代码来显示 WordPress 帖子类别,但它输出的是父类别,而不是子类别。这可以修改为显示子类别吗?

<div class="category">' . get_category(get_query_var('cat'))->name . '</div>

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    使用此代码按 ID 获取子类别

        <?php $cats = get_the_category($post->ID);
    $sep = '';
    foreach( $cats as $cat ) {
      $subcats = get_categories('child_of='.$cat->term_id);
      if($subcats) {
        foreach( $subcats as $subcat )
          {   echo $sep . $subcat->name; $sep = ', '; }
      }
    }
    ?>
    

    【讨论】:

    • 感谢您的代码。这对于获取子类别列表非常有用,但我希望帖子的特定子类别显示在图像上,就像日期一样。这是我的完整代码:if (has_post_thumbnail( $post-&gt;ID ) ) { $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post-&gt;ID ), 'single-post-thumbnail' ); echo '&lt;a href="' . get_the_permalink() . '"&gt;&lt;div class="thumb" style="background: url(' . $image[0] . ') center no-repeat;"&gt;&lt;div class="date"&gt;' . get_the_date('M d, y') . '&lt;/div&gt;&lt;/div&gt;&lt;/a&gt;'; }
    【解决方案2】:

    如果您想显示 WordPress 类别列表,请使用此代码

     <div class="category"> <?php _e('Categories:'); ?> <?php wp_list_cats(); ?></div>
    

    或者你想调用 woocomerce 类别然后调用使用此代码

     <div class="category">
            <?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 = 1;      // 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 '<br /><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  $sub_category->name ;
                        }   
                    }
                }       
            }
            ?>
        </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多