简体   繁体   English

WordPress-在自定义类别浏览器中显示父类别

[英]WordPress - Show Parent Category In Custom Category Walker

I'm in the process of creating a custom category walker for my WordPress site. 我正在为我的WordPress网站创建自定义类别浏览器。 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. 我需要做的是创建一个变量,该变量将替换第一个输出(start_lvl)中的$ parent_category变量,该变量将显示子菜单的父类别。 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. 为了将来参考,我通过重组Walker来实现这一点,因此可以使用现有的$ cat_name变量。 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>";
    }

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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