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.
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.
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:
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:
This extends the
Walker_Category
class so that we can callparent::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 theelse
code was taken fromWalker_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:Please use this code
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