簡體   English   中英

顯示自定義分類術語的子級

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM