我正在为我的WordPress网站创建自定义类别漫游器。这将是我的主菜单的一部分,并将显示所有顶级类别与下拉菜单中显示的类别的子项。
我想要做的是创建一个超级菜单效果,因此我想在一个范围内的下拉列表中重复父类别名称,以便我可以将其用作标题。
到目前为止,我的助行器的代码如下:
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_lvl)中的$ parent_category变量,该变量将显示子菜单的父类别。我无法弄清楚如何做到这一点。
任何帮助都将不胜感激。
谢谢, 詹姆斯
答案 0 :(得分:0)
为了将来的参考,我通过重构我的助行器实现了这一点,所以我可以使用现有的$ 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>";
}
}
}