Ask your WordPress questions! Pay money and get answers fast! (more info)

How can I display categories in hierarchy in a custom menu?

  • REFUNDED

How can I display categories in hierarchical order in the metabox when making a new custom menu?... without editing any core files! Basically make the metabox for categories look like the edit post category metabox.

See this thread...
http://wordpress.org/support/topic/auto-category-hierarchy-order-for-custom-menus?replies=0#post-4609715

If someone knows how to whip up a plugin that would be awesome :)

Answers (3)

2013-09-04

Hariprasad answers:

Hi,

Let me know where you want to display this categories in hierarchical order. I mean which are you want to display this

2013-09-04

Liam Bailey answers:

I have read the links you have sent us to. You want your categories in hierarchical order in your nav-menus built using the WP custom menu interface. I can't do that, but you could replace your call to wp_nav_menu with a call to wswp_nav() and create the following functions in your functions.php file.

function wswp_nav() {
$menu = '<div id="mainMenu" class="ddsmoothmenu"><ul id="menu-main-menu" class="menu">';
$menu .= wp_nav_menu(array('container' => '','theme_location' => 'main-menu', 'echo' => false,'items_wrap' => '%3$s'));
//items_wrap takes away all the wrapping, we build the wrapping manually in the line above, take this wrapping from your own menu
//before making this change
$menu .= wp_list_categories(array('hierarchical' => true,'echo' => false,'title_li' => '');
//add in the classes you need to match up with the menu etc
$menu .= '</ul></div>';

echo $menu;
}


Alternatively dont make the call to wp_list_categories there, make it:

function wswp_nav() {
$menu = '<div id="mainMenu" class="ddsmoothmenu"><ul id="menu-main-menu" class="menu">';
$menu .= wp_nav_menu(array('container' => '','theme_location' => 'main-menu', 'echo' => false,'items_wrap' => '%3$s'));
//items_wrap takes away all the wrapping, we build the wrapping manually in the line above, take this wrapping from your own menu
//before making this change
$menu .= wswp_menu();
$menu .= '</ul></div>';

echo $menu;
}


and use wswp_menu() to build any nav elements you want for example:

function wswp_menu() {
foreach(get_categories(array('parent' => 0)) as $parent_cat) {
?><li class='class-for-top-level'><?php echo get_category_link($parent_cat->term_id);
if (get_categories(array('child_of' => $parent_cat->term_id)) {
?><ul class='class-for-sub-menu-ul'><?php
foreach(get_categories(array('child_of' => $parent_cat->term_id) as $child_cat) {
<li class='class-for-sub-levels'><?php echo get_category_link($child_cat->term_id); ?></li><?php

}
?> </ul> <?php
}//end if child_cat ?>
</li><?php
}
}


And its not too far a stretch to turn that into an iterator to nest your levels as far as you want to go. I am happy to take on paid work my email is [email protected] if you want to discuss this avenue.

2013-09-04

Arnav Joy answers:

where you want to display it?