0

I have this code. But when i enter in subcategory which has no own sub, it disappears. I want if has no subs return parent's subs.

$category->category_parent and $category->parent aren't returning parent cat id.

<div style="margin: 0 0 20px 0;">
      <?php $thisCat = get_categories(array('child_of'=>$_GET['cat']));
      if(empty($thisCat)){
                      $thisCat = get_categories(array('parent'=>$_GET['cat']));
      }
                foreach($thisCat as $cat) { ?>
                <div style="margin: 0 25px 0 0; display: inline-block;">
                <a style="font-size: 10pt; color: #777; font-weight: normal;" href="/?cat=<?php echo $cat->cat_ID; ?>"><?php echo $cat->name; ?></a><span style="font-size: 7pt; font-weight: bold; background: #f47a6d; padding: 0 4px 0 4px; color: white; border-radius: 1px; margin: -15px 0 0 -5px; position: absolute; text-align: center;"><?php echo $cat->count; ?></span>
                    </div>
                <?php }
      ?></div>

Example:

  • Main category

    • Sub category 1 (has no sub cats)
    • Sub category 2 (has no sub cats)

When i go to "Sub category 1" and "Sub category 2" url i want to show "Main category's" sub categories ("Sub category 1", "Sub category 2") if "Sub Category 1" and "Sub Category 2" has no its own sub categories.

0

Your question is not that clear, I suggest you to improve your question.

You can try adding array parameter hide_empty to false in this case "0" so that it shows all the category even the one with no post.

$thisCat = get_categories(array('child_of'=>$_GET['cat'], 'hide_empty'=>0));

You can also refer the WordPress get_categories Codex

Hope this helps!

1
0

This solved problem. It gives subcategory's parent category ID

<?php get_term_by('term_taxonomy_id',get_query_var('cat'),'category'); ?>
0

You can also use this code:

$thisCat = get_categories(array('parent'=> $_GET['cat']));
1
  • Please don't just dump code, add some explanation on how this bit of code with solve the issue. – Mayeenul Islam Jul 30 '17 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.