Php WordPress-在自定义类别中显示父类别

Php WordPress-在自定义类别中显示父类别,php,wordpress,Php,Wordpress,我正在为我的WordPress网站创建一个自定义的类别漫游器。这将是我的主菜单的一部分,并将显示所有顶级类别以及下拉菜单中显示的类别的子类别 我想做的是创建一个大菜单效果,因此我想在跨度内的下拉列表中重复父类别名称,以便我可以将其用作标题 到目前为止,我的walker的代码如下: class Nav_Catwalker extends Walker_Category { // Configure the start of each level function start_lv

我正在为我的WordPress网站创建一个自定义的类别漫游器。这将是我的主菜单的一部分,并将显示所有顶级类别以及下拉菜单中显示的类别的子类别

我想做的是创建一个大菜单效果,因此我想在跨度内的下拉列表中重复父类别名称,以便我可以将其用作标题

到目前为止,我的walker的代码如下:

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>";
    }

}
我需要做的是创建一个变量来替换第一个输出start\u lvl中的$parent\u category变量,该变量将显示子菜单的父类别。我不知道该怎么做

任何帮助都将不胜感激

谢谢,
James

为了便于将来参考,我通过重组walker实现了这一点,以便可以使用现有的$cat_name变量。代码如下

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>";
    }

}

}