简体   繁体   中英

WordPress - Show Parent Category In Custom Category Walker

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

For future reference, I achieved this by restructuring my walker so I could use the existing $cat_name variable. The code is below.

class Navigation_Catwalker extends Walker_Category {

// Configure the start of each level
function start_lvl(&$output, $depth = 0, $args = array()) {
    $output .= "";
}

// Configure the end of each level
function end_lvl(&$output, $depth = 0, $args = array()) {
    $output .= "";
}

// Configure the start of each element
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0) {

    // Set the category name and slug as a variables for later use
    $cat_name = esc_attr( $category->name );
    $cat_name = apply_filters( 'list_cats', $cat_name, $category );
    $cat_slug = esc_attr( $category->slug );

    // Configure the output for the top list element and its URL
    if ( $depth === 0 ) {
        $link = '<a class="parent-category-dropdown" href="' . esc_url( get_term_link($category) ) . '"' . '>' . $cat_name . '</a>';
        $indent = str_repeat("\t", $depth);
        $output .= "\t<li class='parent-category " . $cat_slug . "'>$link\n<div class='category-dropdown'>\n<span class='parent-category-title'>" . $cat_name . "</span>\n$indent<ul class='submenu'>\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() ) {
    if ( $depth === 0 ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n</div>\n";
    }
    if ( $depth > 0 ) {
        $output .= "</li>";
    }

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM