List all categories but only have grandchildren anchored (WordPress)

I’m currently using this to list all the categories:

<?php 
    $args = array (
    'menu' => 'secondary-navigation',
    'sort_column' => 'menu_order',
    'container' => 'false', 
    'title_li' => 0,
    'hide_empty' => 0
    );
    wp_list_categories($args);
?>

This will simply list all the categories in hierarchy and with each item anchored.

Read More

My categories are actually set-up like this:

Parent
    -> Child
        -> Grandchild
            -> Great Grandchild

My problem is that I only want the Great Grandchildren to have anchors. I don’t want Parent, Child or Grandchild to have anchors.

Any suggestions would be appreciated.

Related posts

Leave a Reply

3 comments

  1. You could simply disable links using pure CSS without changing any php code at all.
    Check the following code, it will change the mouse cursor, disable the linking function, and hide the underline style:

    .cat-item a, .cat-item .cat-item .cat-item .cat-item a {
          cursor: default;
          pointer-events: none;
          text-decoration: none;
    }
    .cat-item .cat-item .cat-item a {
          cursor: pointer;
          pointer-events: auto;
          text-decoration: underline;
          /* also add here any special style for grandchildren categories */
    }
    

    The result will be exactly as you require, only the grandchild category appears to be anchored.

    Hope that answers your question

  2. As mentioned by Lleo Holmes in the comments, the best approach is to create a custom Walker Class to implement this functionality. I took a stab at it, and came up with the following:

    class Depth_Category_Walker extends Walker_Category {
        private $allowed_depths;
    
        function __construct( $depths_to_link = array() ) {
            $this->allowed_depths = !is_array($depths_to_link) ? array($depths_to_link) : $depths_to_link;
        }
    
        function start_el( &$output, $category, $current_depth = 0, $args = array(), $id = 0 ) {
            extract($args);
            if( in_array( $current_depth, $this->allowed_depths ) ) {
                parent::start_el( $output, $category, $current_depth, $args, $id );
            } else {
                $cat_name = esc_attr( $category->name );
                $cat_name = apply_filters( 'list_cats', $cat_name, $category );
                if ( !empty($show_count) )
                    $cat_name .= ' (' . intval($category->count) . ')';
    
                if ( 'list' == $args['style'] ) {
                    $output .= "t<li";
                    $class = 'cat-item cat-item-' . $category->term_id;
                    if ( !empty($current_category) ) {
                        $_current_category = get_term( $current_category, $category->taxonomy );
                        if ( $category->term_id == $current_category )
                            $class .=  ' current-cat';
                        elseif ( $category->term_id == $_current_category->parent )
                            $class .=  ' current-cat-parent';
                    }
                    $output .=  ' class="' . $class . '"';
                    $output .= ">$cat_namen";
                } else {
                    $output .= "t$cat_name<br />n";
                }
            }
        }
    }
    

    This extends the Walker_Category class so that we can call parent::start_el() to produce the link if it’s at the appropriate depth. The constructor accepts an array of depths that contains the levels you wish to display links. Any depth that falls outside that array would be rendered as plain-text. Note that the else code was taken from Walker_Category::start_el, so this could break in future releases, if the base class is ever modified.

    The above class can be used by calling wp_list_categories like:

    <?php
       $args = array(
           'orderby' => 'menu_order',
           'title_li' => 0,
           'hide_empty' => 0,
           'walker' => new Depth_Category_Walker(2)
       );
    ?>
    <ul>
       <?php wp_list_categories( $args ); ?>
    </ul>
    
  3. Please use this code

    $categories = get_categories(array(
            'child_of' => 9,
            'hide_empty' => false
        ));
    foreach($categories as $cat){
        echo $cat->name;
        echo '<br />';
    }
    

    this code is only tested for the two level category.

    Replace 9 with your category id like with “Grandchild” cateogry id.
    This code is not tested but this is work

    Hope this helpful for you