2010-06-11 125 views
2

我想只有那些與他們的孩子類別某些子類別,而無需使用child_of=顯示WordPress的母公司類別

我試圖顯示,但我只能得到列表中的父類顯示子類別不是其父類別名稱。

<?php 

$querystr = "SELECT wp_terms.name, wp_terms.term_id, wp_terms.name FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.parent !=0 "; 
$cat_child = $wpdb->get_results($querystr, OBJECT); 
var_dump($cat_child); 
foreach ($cat_child as $category) { 
     echo $category->name. ' , '; 
     } 
    ?> 

幫助我..謝謝

+0

因此,您只想顯示給定類別的* parent *? – TheDeadMedic 2010-06-11 09:26:02

+0

是的,我想顯示該子類別的父類別 – Soarabh 2010-06-11 09:29:15

回答

2

做這個

<?php 

          $querystr = "SELECT wp_terms.name, wp_terms.term_id, wp_terms.name FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.parent !=0 "; 
          $cat_child = $wpdb->get_results($querystr, OBJECT); 
          var_dump($cat_child); 
          echo '<ul>'; 
          foreach ($cat_child as $category) { 
           $cat_id = intval($category->term_id); 
           echo '<li>'; 
            echo get_category_parents($cat_id , TRUE , ' <br/> '); 
           echo '</li>'; 
          } 
          echo '</ul>'; 
        ?> 

完成謝謝

0

如果不希望使用 「child_of」 參數,那麼您的問題可以通過使用兩個迴路來解決,其中一個用於displaying parent categories和其他用於displaying its direct child categories.

// get_categories() function will return all the categories 
     $upaae_categories = get_categories(array(
     'orderby' => 'name', 
     'order' => 'ASC' 
     )); 

     foreach($upaae_categories as $single_cat) { 
     if($single_cat->parent < 1) // Display if parent category and exclude child categories 
     { 
    echo 'Parent: '.$single_cat->name; 
    // now get all the child categories 
    $child_categories=get_categories(
     array('parent' => $single_cat->term_id) 
    ); 
    if(sizeof($child_categories)>0){ /* this is just for ensuring that this parent category do have child categories otherwise a category cannot be a parent if does not have any child categories*/ 
    echo '###childs###</br>' 
    foreach ($child_categories as $child) { 

     echo $child->name.'</br>'; 
    }// end of loop displaying child categories 
    } //end of if parent have child categories 

     } 
     }