【问题标题】:Grouping Parent and child categories for post and displaying为发布和显示分组父子类别
【发布时间】:2017-10-18 01:35:57
【问题描述】:

我看到了一些关于如何显示父子类别的问题,但似乎找不到我正在做的事情的解决方案。

我正在使用带有多个类别和子类别的默认 wordpress 帖子作为我的示例

新闻 - 旅行 - 世界 - 商业

文章 -一般

案例研究 -托马斯库克 - 易捷航空

帖子可以有任意数量的类别,但我要做的是将子项与其父项分组,以便在显示帖子时,例如,显示用于以下每个帖子的父项和子项类别格式(父项后的冒号和除最后一个子项后的每个子项后的连字符)。

新闻:旅游 - 世界

目前我正在使用以下代码;

<?php 
$categories = get_the_category();
$separator = ' - ';
$output = '';
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        $output .= '<a class="blog-panel-cat" href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
    }
    echo trim( $output );
}
?>

但他只是一个接一个地输出一个类别,无法分组,因此我可以在父级之后添加“:”,并用“-”分隔该父级的子级,并在下一个父级之前添加一个双空格。

任何帮助表示赞赏

谢谢

伊恩

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我已经为在 Rest Api 中显示做了类似的事情,所以这一切只是将所有类别安排到分层数组/对象中。它只适用于一年级的孩子。

    $allcats = get_categories(); // get ALL categories 
    // $allcats = get_the_category(); // get only categories assigned to post
    $parents = $all_ids = array();
    
    //  find parents
    foreach ($allcats as $cats) {
        if ($cats->category_parent === 0 ) {
            $cats->children = array();
            $parents[] =  $cats;
        }
    }
    
    //  find childredn and assign it to corresponding parrent  
    foreach ($allcats as $cats) {
        if ($cats->category_parent != 0) {
            foreach ($parents as $parent) {
                if ($cats->category_parent === $parent->term_id) {
                    $parent->children[] = $cats;
                }
            }
        }
    }
    

    所以结果会是这样的:

    [{
       term_id: 50,
       name: "Main Cat",
       slug: "main-cat",
       term_group: 0,
       term_taxonomy_id: 50,
       taxonomy: "category",
       description: "",
       parent: 0,
       count: 77,
       filter: "raw",
       term_order: "1",
       cat_ID: 50,
       category_count: 77,
       category_description: "",
       cat_name: "main cat",
       category_nicename: "main_cat",
       category_parent: 0,
       children: [
       {
          term_id: 49,
          name: "Sub Cat",
          slug: "sub-cat",
          term_group: 0,
          term_taxonomy_id: 49,
          taxonomy: "category",
          description: "",
          parent: 50,
          count: 43,
          filter: "raw",
          term_order: "1",
          cat_ID: 49,
          category_count: 43,
          category_description: "",
          cat_name: "sub_cat",
          category_nicename: "Sub Cat",
          category_parent: 50
        }, 
        {etc other children}
       ]
     }]
    

    希望这对某人有帮助...

    【讨论】:

      【解决方案2】:

      几个月前我处理过完全相同的案例,这是我的解决方案

      //first  part
      $allcats = get_the_category();
      $parent_id = $all_ids = array();
      
      foreach ($allcats as $cats) {
          //get all category id first
          $all_ids[] = $cats->term_id;
      
          // get top parent category
          if ($cats->category_parent === 0 ) {
              $parent_id[] =  $cats->term_id;
          }
      }
      
      //second part
      $separator = ' &raquo; ';
      $term_ids = implode( ',' , $all_ids );
      
      $post_categories  = '';
      foreach ($parent_id as $parents) {
          $parent_name    = get_cat_name($parents);
          $parent_link    = get_category_link($parents);
          $parent_cat     = '<span class="parent"><a href="'.$parent_link.'">'.$parent_name.'</a></span>';
      
          //get all sub category with certain ids
          $child_cats = wp_list_categories(
              array(
              'child_of' =>  $parents, 
              'title_li' => '',
              'style'    => 'none',
              'echo'     => false,
              'taxonomy' => 'category',
              'include'  => $term_ids,
      
              )
          );
          $child_cats = rtrim( trim( str_replace( '<br />',  $separator, $child_cats ) ), $separator );
          $hcat .= '<b>'.$parent_cat.'</b> &raquo; '.$child_cats.'<br>';
      }
      
      echo $post_categories;
      

      经过测试,希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-15
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多