I'm in the process of creating a custom category walker for my WordPress site. This will be part of my main menu and will show all top level categories with the children of the categories shown in a dropdown menu.
What I'd like to do is create a mega menu effect and as a result I'd like to repeat the parent category name in my dropdown inside a span so I can use it as a heading.
The code for my walker so far is as follows:
class Nav_Catwalker extends Walker_Category {
// Configure the start of each level
function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "<div class='sub-categories'>\n<span>" . $parent_category . "</span>\n$indent<ul class='sub-nav'>\n";
}
// Configure the end of each level
function end_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n</div>\n";
}
// Configure the start of each element
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0) {
// Set the category name as a variable for later use
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
// Configure the output for the top list element and its URL
if ( $depth === 0 ) {
$link = '<a href="#">' . $cat_name . '</a>';
$output .= "\t<li class='parent-category'>$link\n";
}
// Configure the output for lower level list elements and their URL's
if ( $depth > 0 ) {
$link = '<a href="' . esc_url( get_term_link($category) ) . '"' . '>' . $cat_name . '</a>';
$output .= "\t<li class='sub-category'>$link\n";
}
}
// Configure the end of each element
function end_el(&$output, $page, $depth = 0, $args = array() ) {
$output .= "</li>";
}
}
What I need to do is create a variable that will replace the $parent_category variable in the first output (start_lvl) that will show the parent category of the sub-menu. I can't figure out how to do this.
Any help would be appreciated.
Thanks, James