php – Category view depending on parent and cild categories

Question

I try to get a category view depending on parent or child categories.

The code im using in category.php for dependencies looks like this and is working quite well, except one logical error.

Differences I need:

Parent category with own posts and child categories -> works

Parent category without own posts but child categories -> the logic is not right

Child category with posts -> works

Category -> works

<?php
 if ( have_posts() ) {
 $this_category = get_category($cat);
?>

<?php if (($this_category->category_parent == 0) && ($this_category->category_count > 0)) { ?>
 <p>i am parent category with own posts and child categories</p>

<?php } elseif (($this_category->category_parent == 0) && ($this_category->category_count == 0)) { ?>
 <p>i am parent category without own posts but child categories</p>

<?php } elseif (($this_category->category_parent != 0) && ($this_category->category_count > 0)) { ?>
 <p>i am child category</p>

<?php } else { ?>
 <p>i am category</p>
<?php } ?>


<?php
} else {
 get_template_part( 'loop-templates/content', 'none' );
}
?>

Can someone help?

0
HEN 2 months 2021-07-31T00:59:13-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse