0

I need to show in the each post his parent category and sub category, at the moment I use this code:

<?php
$categories = get_the_category();

echo 'Parent Category: <a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a> \n';

echo 'Sub Category: <a href="' . esc_url( get_category_link( $categories[1]->term_id ) ) . '">' . esc_html( $categories[1]->name ) . '</a>';
?>

The problem is that this code gives categories[0] and categories[1] values based on their order. In some post the order is Parent -> Sub, in others is Sub -> Parent (don't know why). So when I access $categories[1] in some posts I get the parent instead of the sub.

0

Try it like this:

$categories = get_the_category();

echo '<ul>';
echo '<li> Parent Category: ';
foreach( $categories as $category ){
     if($category->parent != 0){
          $parent_category = get_term( $category->parent );
          echo '<a href="' . esc_url( get_category_link($parent_category->term_id)) . '">' . esc_html($parent_category->name) . ' </a>';
          break; 
     }
}
echo '</li>';
echo '<li>Subcategory: ';
foreach( $categories as $category ){
     if($category->parent != 0){
          echo '<a href="' . esc_url( get_category_link($category->term_id)) . '">' . esc_html($category->name) . ' </a>';
     }
}
echo '</li></ul>';

This works pretty well if your post's categories have the same parent category (e.g Your post has the categories 'child_1' and 'child_2' which are both children of the category 'parent_1'). It checks each of your post's categories. In the first foreach-loop, as soon as a category has a parent category it will be echoed and the loop will be left because of the break. But if your post has multiple categories with different parent categories (e.g Your post has the category 'child_A_1' which is a child of 'parent_A', and the category 'child_B_1' which is a child of 'parent_B') this code won't work correctly since it would only echo the parent-category of the first subcategory that occurs in the first foreach-loop.

0

You can use array_reverse to fix it so it makes more sense to you.

<?php $categories = get_the_category();

$rev_categories = array_reverse( $categories );

echo 'Parent Category: <a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a> \n';

echo 'Sub Category: <a href="' . esc_url( get_category_link( $categories[1]->term_id ) ) . '">' . esc_html( $categories[1]->name ) . '</a>';
?>

If you are absolutely sure that you're never going to have an instance where $categories[1] won't exist, then your code will be fine, but I'd want to change it, run a foreach loop on all your categories, changing the label if the $key is 0.

foreach ( $rev_categories as $key => $category ) {
    echo ( $key == 0 ? 'Parent' : 'Sub' ) . ' Category: <a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
}

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.