我想只显示父类别中包含某个子类别的子类别而不使用child_of=
我试图显示,但我只能获得子类别列表而不是其父类别名称。
<?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 :(得分: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>';
?>
谢谢
答案 1 :(得分: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
}
}