将父子类别分组以进行发布和显示 [英] Grouping Parent and child categories for post and displaying

查看:16
本文介绍了将父子类别分组以进行发布和显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

新闻- 旅行- 世界- 商业

文章-一般

案例研究-托马斯·库克- Easy Jet

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

新闻:旅游 - 世界

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

term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View%s', 'textdomain' ), $category->name ) ) 中的所有帖子.'">'.esc_html( $category->name ) .'</a>'.$分隔符;}回声修剪($输出);}?>

但他只是一个接一个地输出一个类别,无法分组,所以我可以在父级之后添加 ':' 并用 '-' 分隔该父级的子级,并且在下一个父级之前还有一个双空格.

感谢任何帮助

谢谢

伊恩

解决方案

我为在 Rest Api 中显示做了类似的事情,所以所做的只是将所有类别排列成分层数组/对象.而且只对一年级的孩子有效.

$allcats = get_categories();//获取所有类别//$allcats = get_the_category();//只获取分配给帖子的类别$parents = $all_ids = array();//找到父母foreach ($allcats 作为 $cats) {如果($cats->category_parent === 0){$cats->children = array();$parents[] = $cats;}}//找到 childredn 并将其分配给相应的父级foreach ($allcats 作为 $cats) {如果 ($cats->category_parent != 0) {foreach ($parents 作为 $parent) {if ($cats->category_parent === $parent->term_id) {$parent->children[] = $cats;}}}}

所以结果将类似于:

<代码>[{term_id: 50,name: "主猫",弹头:主猫",term_group: 0,term_taxonomy_id: 50,分类:类别",描述: "",父母:0,计数:77,过滤器:原始",term_order: "1",cat_ID: 50,类别计数:77,类别描述:"",cat_name: "主猫",category_nicename: "main_cat",category_parent: 0,孩子们: [{term_id: 49,name: "子猫",slug: "子猫",term_group: 0,term_taxonomy_id: 49,分类:类别",描述: "",父母:50,计数:43,过滤器:原始",term_order: "1",cat_ID: 49,类别计数:43,类别描述:"",cat_name: "sub_cat",category_nicename: "子猫",category_parent:50},{等其他孩子}]}]

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

I have seen a few questions for how to display parent and child categories but can't seem to find a solution for what I am doing.

I'm using the default wordpress post with several categories and child categories for an example I have

News - Travel - World - Business

Article -General

Case Study -Thomas Cook - Easy Jet

The post can have any number of categories but what I am looking to do is group the child with its parent so that when I display a post it, for example, displays the Parent and Child categories used for each post in the following format (colon after the parent and hyphen after each child apart from the last).

News: Travel - World

Currently I am using the following code;

<?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 );
}
?>

But his just outputs one category after the other with no way of grouping so I can add ':' after the parent and separate the child for that parent with '-' and also a double space before the next parent.

Any help appreciated

Thanks

Ian

解决方案

I have done something similar for showing in Rest Api, so all this does is arranges all of the categories into hierarchical arrays/objects. And it only works for first-grade children.

$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;
            }
        }
    }
}

So the result will be something like:

[{
   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}
   ]
 }]

Hope this helps someone...

这篇关于将父子类别分组以进行发布和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆