2010-06-11 157 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 

     } 
     }