0

I am setting up non-for-profit domestic violence support website and struggle to find a way to create active category parent and it children links in pages sidebar.

What is the ideal solution to dynamically display category parent and it's children?

  • I'm a little bit unclear with your question... Do you want to display all parent categories and child categories in hierarchical order? OR Do you want to display parent category and child category of the only category to which that particular post belongs to? – dhirenpatel22 Jun 28 '18 at 4:28
  • 1. Do you want to display parent category and child category of the only category to which that particular post belongs to? – William Jerome Jun 28 '18 at 5:03
1
$current_cat_id = get_queried_object_id();
$kids = get_terms([
    'taxonomy' => get_queried_object()->taxonomy,
    'parent'   => $current_cat_id,
]);

<?php

$terms = get_terms([
    'taxonomy' => get_queried_object()->taxonomy,
    'parent'   => get_queried_object_id(),
]);

echo '<div style="height: 200px; text-transform: uppercase; border:1px solid #666666; padding:10px; overflow-y: scroll;">
<ul>
foreach ( $terms as $term) {
    echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';  
}
echo '</ul>
<br />';
| improve this answer | |
1

Do you want to list all of the Sub-Categories i.e Child Categories from the Current Category?

If yes, then you can use the following simple code snippet that will help you to list all the sub-categories from the current post’s parent category.

Code for Getting subcategories of current category

<ul id="all-sub-categories">

<?php
   $postcat = get_the_category( $post->ID );
   foreach ($postcat as $cat) {
      if ($cat->parent != 0) {
        echo "<li><a href='" . get_category_link( $cat->term_id ) . "'>" . $cat->name . "</a></li>";
      }
   }
?>

</ul>

That’s it.

| improve this answer | |
1

Finally found a workaround...add below code in your sidebar template. You can replace 'category' taxonomy with your custom taxonomy.

<?php
        // get Post Object outside
        $queried_object = get_queried_object();
        if ( $queried_object ) {
            $post_id = $queried_object->ID;
        }
        //Returns All Term Items for "my_taxonomy"
        $term_list = wp_get_post_terms($post_id, 'category', array("fields" => "all"));
        $term_list_id = wp_get_post_terms($post_id, 'category', array("fields" => "ids"));
        echo '<ul>';
        foreach($term_list as $term) {
            $has_parent = $term->parent;
            $has_childrens = get_terms( $term->taxonomy, array( 'parent'    => $term->term_id, 'hide_empty' => false) );
            // If it have parents...
            if ( $has_parent ) {
                // If parent category is already selected
                if(in_array($has_parent, $term_list_id)){
                    // Do nothing
                }else{
                    // If parent category is not selected
                    $parent_id = $term->parent;
                    $parent_term = get_term( $parent_id, 'category' );
                    // Display Parent
                    echo '<li><a href="' . get_category_link( $parent_id ) . '">' . $parent_term->name.'</a></li>';
                    echo '<ul>';
                    // Display Children
                    $childrens = get_categories( array('child_of' => $parent_id), 'category' );
                    foreach($childrens as $children) {
                        echo '<li><a href="' . get_category_link( $children->term_id ) . '">' . $children->name.'</a></li>';
                    }
                    echo '</ul>';
                }
            }elseif ( $has_childrens ) {
                // If it have childrens...
                // display parent
                echo '<li><a href="' . get_category_link( $term->term_id ) . '">' . $term->name.'</a></li>';
                    $childrens = get_categories( array('child_of' => $parent_id), 'category' );
                    echo '<ul>';
                // Display children
                foreach($has_childrens as $has_children) {
                    echo '<li><a href="' . get_category_link( $has_children->term_id ) . '">' . $has_children->name.'</a></li>';
                }
                echo '</ul>';
            }else{
                // No Parent No Child
                echo '<li><a href="' . get_category_link( $term->term_id ) . '">' . $term->name.'</a></li>';
            }
        }
        echo '</ul>';
    ?>

Hope this works..!!

| improve this answer | |

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.