List all categories but only have grandchildren anchored (Wordpress) List all categories but only have grandchildren anchored (Wordpress) wordpress wordpress

List all categories but only have grandchildren anchored (Wordpress)


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


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_name\n";            } 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>


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