简体   繁体   English

仅获取自定义帖子类型的子条款

[英]Get only child terms of custom post type

I have category "trekking" with others chld categories.我与其他 chld 类别有“徒步旅行”类别。 I need get only trekking child categories but i can't do it.我只需要获得徒步儿童类别,但我做不到。 This is my code (works fine, only I must exclude all the others categories but that is not a good/dynamic solution).这是我的代码(工作正常,只有我必须排除所有其他类别,但这不是一个好的/动态解决方案)。 Thanks!谢谢!

<ul class="fordesktop">
<?php $customPostTaxonomies = get_object_taxonomies('portfolio');
$category = get_category_by_slug( 'trekking' );
if(count($customPostTaxonomies) > 0)
{
     foreach($customPostTaxonomies as $tax)
     {
         $args = array(
              'orderby' => 'name',
              'show_count' => 0,
              'pad_counts' => 0,
              'hierarchical' => 0,
              'taxonomy' => $tax,
              'title_li' => '',
              'child_of' => $category,
             'exclude' => '10, 19, 20, 21, 22, 26, 29, 30, 31, 35, 36, 37, 41, 42, 43, 44',
             'hide_title_if_empty' => 0
            );

         wp_list_categories( $args );
     }
} ?></ul>

Another way is the next code, but I can't hide empty terms:另一种方法是下一个代码,但我不能隐藏空词:

<?php
$term_id = 10;
$taxonomy_name = 'portfolio_category';
$termchildren = get_term_children( $term_id, $taxonomy_name );
 
echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?> 

Try using get_term_children() :尝试使用get_term_children()

// This will give you an array with the ID's of all child categories of 'trekking':
$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$child_cat_ids = get_term_children($cat_id, 'category');

An alternative approach, if you prefer a list of category objects, instead of IDs would be:如果您更喜欢类别对象列表而不是 ID,另一种方法是:

$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$child_cats = get_categories(['parent' => $cat_id]);

See more info here: https://developer.wordpress.org/reference/functions/get_term_children/ https://developer.wordpress.org/reference/functions/get_categories/ See more info here: https://developer.wordpress.org/reference/functions/get_term_children/ https://developer.wordpress.org/reference/functions/get_categories/

EDIT:编辑:

if you want to use wp_list_categories() in your code, try this:如果你想在你的代码中使用wp_list_categories() ,试试这个:

$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$args = array(
        'child_of'   => $cat_id,
);
wp_list_categories($args);

EDIT 2:编辑2:

Adjusted the solution for custom taxonomies:调整了自定义分类的解决方案:

$tax = get_term_by('slug', 'trekking', 'portfolio');
$tax_id = $tax->term_id;
$args = array(
'child_of'   => $tax_id,
'taxonomy' => 'portfolio',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,     
'title_li' => '',
'hide_title_if_empty' => 0
);
wp_list_categories($args);

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

相关问题 如何获取当前职位类型的唯一子条款? - How to get only child terms of current post type? PHP将WP条款子条款导入自定义帖子类型 - PHP Import WP Terms child term into custom post type 获取数组中自定义帖子类型的自定义分类的术语 - Get the terms of a custom taxonomy of custom post type in an array wordpress:如何在单个自定义帖子类型上获取特定的元术语 - wordpress : how to get specific meta terms on single custom post type 如何仅为自定义帖子类型获取术语或分类法 - How to get terms or taxonomies only for custom post types Wordpress 永久链接重写,用于自定义分类法和在 URL 中使用父项和子项层次结构的帖子类型 - Wordpress permalink rewrite for custom taxonomy and post type that uses parent and child terms hierarchy in URLs 按计数获取自定义分类术语列表,其中包含自定义 post_type 帖子 - Get the list of custom taxonomy terms by count which have custom post_type posts 如何仅显示自定义帖子类型的子帖子? - How to display only the child posts of a custom post type? 仅获取 Woocommerce 产品标签的发布条款? - Get only post terms for Woocommerce product tags? 在Wordpress中的帖子旁边显示自定义帖子类型分类法术语 - Showing Custom Post Type Taxonomy Terms Next to Post in Wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM