[英]Get only child terms of custom post type
我與其他 chld 類別有“徒步旅行”類別。 我只需要獲得徒步兒童類別,但我做不到。 這是我的代碼(工作正常,只有我必須排除所有其他類別,但這不是一個好的/動態解決方案)。 謝謝!
<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]);
See more info here: 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.