Reputation: 2950
I want to display only those the parent category which have some child category with their child category without using child_of=
I was trying to display but i am only able to get the list of child category not their parent category name.
<?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. ' , ';
}
?>
Help me.. Thanks
Upvotes: 2
Views: 6644
Reputation: 90
If you don't want to use "child_of" argument then your problem can be solved by using two loops one for displaying parent categories and other for 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
}
}
Upvotes: 0
Reputation: 2950
Done by doing this
<?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>';
?>
Thank you
Upvotes: 2