[英]How to retrieve all children terms of a parent terms of a custom taxonomy according to hierarchy in WordPress?
[英]Show children of custom taxonomy terms
我有WP Jb管理器插件,我相信這只是工作的自定義帖子類型。 我已經啟用類別創建了一些類別。
我只需要顯示每個類別的子代,而不是整個類別列表即可。
我有以下代碼:
<?php
$terms = get_terms( 'job_listing_category', 'orderby=count&hide_empty=0' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>
這樣輸出所有類別(父級和子級)的列表:
父類別為粗體:辦公,工業和建築。 我想選擇其中一個,僅顯示該類別的子級。
例如: get_category('industrial', 'children_of')
(我知道這不是正確的語法),因此它將導致僅顯示工業類別的子級:
我似乎找不到解決方法-有人可以幫忙嗎?
我設法通過使用以下代碼來做到這一點:
<?php
$terms = get_terms( 'job_listing_category', 'parent=59' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>
您可以獲取父類別,然后為每個子類別構造一個列表,如下所示:
<?php
$taxonomies = get_terms(array(
'taxonomy' => 'job_listing_category',
'hide_empty' => false,
'parent' => 0,
));
if (!empty($taxonomies)):
foreach ($taxonomies as $parent) {
$output = '<ul>';
$children = get_terms(array(
'taxonomy' => 'job_listing_category',
'parent' => $parent->term_id,
'hide_empty' => false,
));
foreach ($children as $child) {
$output .= '<li>' . esc_html($child->name) . '</li>';
}
$output = '</ul>';
}
echo $output;
endif;
請注意,該代碼未經測試。 在此處查找更多信息: https : //developer.wordpress.org/reference/functions/get_terms/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.