0rangeJulius
Member
Posted 1 year ago #
I'm attempting to create a category.php page that displays the following:
if(category has no subcategories and no posts){
display category title and description;
}elseif(category has no subcategories and 1 post){
display the post;
}elseif(category has subcategories){
display subcategories as grid;
}else{
display posts as grid;
}
PLEASE help. I've looked all over and there's nothing even CLOSE to helpful out there. The documentation has only made me frustrated in its lacking. Thanks!
for the conditional structure, try and use:
<?php
$cat_id = get_query_var('cat');
$subs = get_categories('child_of=' . $cat_id );
$cat = get_category($cat_id);
$postcount = $cat->count;
if( !subs && $postcount == 0 ) { /*category has no subcategories and no posts -- display category title and description;*/ }
elseif( !subs && $postcount ==1 ) { /*category has no subcategories and 1 post -- display the post;*/ }
elseif( $subs && $postcount == 0 ) { /*category has subcategories -- display subcategories as grid;*/ }
else { /*display posts as grid;*/ }
?>
http://codex.wordpress.org/Function_Reference/get_category
http://codex.wordpress.org/Function_Reference/get_categories
http://codex.wordpress.org/Database_Description#Table:_wp_terms