简体   繁体   English

从当前帖子(自定义帖子类型)中获取子类别和父类别名称(自定义分类)

[英]Get child and parent category name (custom taxonomy) from current post (custom post type)

I am trying to get the parent category and child category from the current post.我正在尝试从当前帖子中获取父类别和子类别。
The custom post type is called assortiment and the custom taxonomy is called assortiment-categorie .自定义帖子类型称为assortiment ,自定义分类称为assortiment-categorie

We have a product called SL524CB – 500kg with parent category Test and subcategory (of Test ) test b .我们有一个名为SL524CB – 500kg的产品,其父类别Test和子类别( Testtest b

Now I did make a loop which outputs the sub category name ( test b ) but we also want the parent category name ( Test ).现在我做了一个输出子类别名称( test b )的循环,但我们还想要父类别名称( Test )。

To output them we want 2 variables like $parent_category and $subcategory so we can output them in our template.对于 output 它们,我们需要 2 个变量,例如$parent_category$subcategory ,因此我们可以在模板中对它们进行 output。

This is the loop we are using right now:这是我们现在使用的循环:

<?php 
    global $post;
    $terms = wp_get_object_terms( $post->ID, 'assortiment-categorie', array('fields'=>'names'));
    $term_id = array_pop( $terms ); //gets the last ID in the array
    echo $term_id;
?>

If someone could help me that would be great, thanks for your time already!如果有人可以帮助我,那就太好了,已经感谢您的时间了!

There is a multitude of ways we can achieve that.我们可以通过多种方式实现这一目标。

@Chris Haas comment is one way to do it. @Chris Haas 评论是一种方法。 Me, I prefer using an alternative way.我,我更喜欢使用另一种方式。

Regarding parents, we can use get_term_parents_list() which will return a specific term's parents.关于父母,我们可以使用get_term_parents_list()来返回特定术语的父母。

Retrieves term parents with separator.使用分隔符检索术语父项。

<?php

$args = array(
    'format' => 'slug',
    'link' => false,
    'inclusive' => false,
);
  
$parents = explode( '/', get_term_parents_list( $term_id, $taxonomy, $args ) );

$î = 0;
foreach( $parents as $parent ) {
    $i++;

    echo $parent;

    if ( $i !== sizeof( $parents ) )
        echo ', ';

};

Regarding children, we can use get_term_children() which will return a specific term's children.关于孩子,我们可以使用get_term_children()它将返回特定术语的孩子。

Merge all term children into a single array of their IDs.将所有术语子项合并到其 ID 的单个数组中。

<?php

$children = get_term_children( $term_id, $taxonomy );

$î = 0;
foreach( $children as $child ) {
    $i++;

    $term = get_term_by( 'id', $child, $taxonomy );

    echo $term->name;

    if ( $i !== sizeof( $children ) )
        echo ', ';

};

Use get_the_terms() .使用get_the_terms()

$categories = get_the_terms( get_the_ID(), 'category' );
echo "<pre>"; print_r($categories); echo "</pre>"; 

If you print $categories then you will have an output like below.如果您打印$categories那么您将有一个 output 如下所示。 where you can determine that $term is parent or child .您可以在其中确定$termparentchild

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 18
            [name] => Test 2
            [slug] => test-2
            [term_group] => 0
            [term_taxonomy_id] => 18
            [taxonomy] => category
            [description] => 
            [parent] => 17
            [count] => 1
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 17
            [name] => Test
            [slug] => test
            [term_group] => 0
            [term_taxonomy_id] => 17
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

)

Try this below code.试试下面的代码。

foreach ( $categories as $key => $category ) {
    if( $category->parent == 0 ){
        echo "Parent => ".$category->name;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有自定义类别/分类的分页自定义帖子类型 - Pagination Custom Post Type with Custom Category/Taxonomy 获取用于查询自定义帖子类型的当前页面自定义分类法 - Get the current page custom taxonomy use to query custom post type 自定义帖子类型的当前分类法中所有帖子的列表 - list of all post from the current taxonomy of custom post type 从自定义帖子类型中删除默认的“类别”分类法 - Remove default “category” taxonomy from custom post type 为自定义帖子类型类别列表添加类“ current_page_item”(注意:该类别属于自定义分类法) - Adding class “current_page_item” for custom post type category list(note: the category is belong in custom taxonomy) Wordpress - 获取自定义帖子类型的类别名称 - Wordpress - get category name of custom post type 如何从ID获取自定义分类父类别名称? - How to get custom taxonomy parent category name from ID? 如何为自定义帖子类型类别获取当前类别的父类别 - How do I get the Parent Category of the current Category Im on for Custom post type Categories 类别页面上的自定义帖子类型的自定义分类法 - Custom taxonomy thumnbails for custom post type on category page 使用ACF自定义帖子类型显示Slug中的分类名称 - Show Taxonomy Name from Slug with ACF Custom Post Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM