仅获取自定义 post 类型的子术语
Get only child terms of custom post type
我有类别“徒步旅行”和其他儿童类别。我只需要获得徒步旅行的子类别,但我做不到。这是我的代码(工作正常,只是我必须排除所有其他类别,但这不是 good/dynamic 解决方案)。谢谢!
<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>
另一种方式是下一个代码,但是我不能隐藏空词:
<?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>';
?>
尝试使用 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');
如果您更喜欢类别对象列表而不是 ID,另一种方法是:
$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$child_cats = get_categories(['parent' => $cat_id]);
在此处查看更多信息:
https://developer.wordpress.org/reference/functions/get_term_children/
https://developer.wordpress.org/reference/functions/get_categories/
编辑:
如果你想在代码中使用 wp_list_categories()
,试试这个:
$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$args = array(
'child_of' => $cat_id,
);
wp_list_categories($args);
编辑 2:
调整了自定义分类法的解决方案:
$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);
我有类别“徒步旅行”和其他儿童类别。我只需要获得徒步旅行的子类别,但我做不到。这是我的代码(工作正常,只是我必须排除所有其他类别,但这不是 good/dynamic 解决方案)。谢谢!
<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>
另一种方式是下一个代码,但是我不能隐藏空词:
<?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>';
?>
尝试使用 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');
如果您更喜欢类别对象列表而不是 ID,另一种方法是:
$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$child_cats = get_categories(['parent' => $cat_id]);
在此处查看更多信息: https://developer.wordpress.org/reference/functions/get_term_children/ https://developer.wordpress.org/reference/functions/get_categories/
编辑:
如果你想在代码中使用 wp_list_categories()
,试试这个:
$cat = get_category_by_slug('trekking');
$cat_id = $cat->term_id;
$args = array(
'child_of' => $cat_id,
);
wp_list_categories($args);
编辑 2:
调整了自定义分类法的解决方案:
$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);