Wordpress custom walker 创建双菜单
Wordpress custom walker creating a double menu
您好,我为我的 wordpress 主题设置了这个自定义助行器来显示我的主菜单。我大约一个月前开始调整它,但此后一直在研究其他一些方面。回到它,我有点迷失了我离开时的位置。
目前我能看到的唯一真正的问题是助行器正在生成双菜单。所以每个 link 显示两次。
有谁知道为什么菜单项会出现两次?
这是自定义助行器:
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$linkName = apply_filters( 'the_title', $item->post_title, $page->ID );
if($linkName=="HOME"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="all" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}
if($linkName=="CONTACT"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="/contact" class="contact" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}
}
function end_el(&$output, $page, $depth = 0, $args = array()) {
$output .= "</li>\n";
}
}
谢谢
这是因为您还没有学会如何使用 if 循环。你有两个,因此将输出两个链接。你只想输出 1 所以:
if($linkName=="HOME"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="all" data-filter=".all">'.$linkName.'</a>';
} elseif($linkName=="CONTACT"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="/contact" class="contact" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}
您好,我为我的 wordpress 主题设置了这个自定义助行器来显示我的主菜单。我大约一个月前开始调整它,但此后一直在研究其他一些方面。回到它,我有点迷失了我离开时的位置。
目前我能看到的唯一真正的问题是助行器正在生成双菜单。所以每个 link 显示两次。
有谁知道为什么菜单项会出现两次?
这是自定义助行器:
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$linkName = apply_filters( 'the_title', $item->post_title, $page->ID );
if($linkName=="HOME"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="all" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}
if($linkName=="CONTACT"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="/contact" class="contact" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}
}
function end_el(&$output, $page, $depth = 0, $args = array()) {
$output .= "</li>\n";
}
}
谢谢
这是因为您还没有学会如何使用 if 循环。你有两个,因此将输出两个链接。你只想输出 1 所以:
if($linkName=="HOME"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="all" data-filter=".all">'.$linkName.'</a>';
} elseif($linkName=="CONTACT"){
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="/contact" class="contact" data-filter=".all">'.$linkName.'</a>';
}else{
$output .= $indent . '<li id="item_'.$item->ID.'"><a href="#" class="'.strtolower($linkName).'" data-filter=".'.strtolower($linkName).'">'.$linkName.'</a>';
}