我正在添加#section
作为我菜单的自定义链接,因为我想在单击该链接时滚动到该部分。当我在主页上时,这很好(该部分在主页上),但当我不在主页上时,我单击它什么也不会发生,因为它将链接视为#section
.
也就是说,当我在第二页时:
http://127.0.0.1/second-page
然后我点击
#section
菜单中的链接,它尝试执行以下操作
http://127.0.0.1/second-page/#section
而不是
http://127.0.0.1/#section
现在,该网站仍在开发中,所以它是通过IP地址设置的。但这不重要。问题是我试图将wordpress后端中的自定义链接设置为
http://127.0.0.1/#section
而在后端,这看起来像那样,在我的菜单上,在前端,我只看到
<a href="#section">Section</a>
控制菜单输出的菜单漫游器如下所示:
<?php
// Allow HTML descriptions in WordPress Menu
remove_filter( \'nav_menu_description\', \'strip_tags\' );
function my_plugin_wp_setup_nav_menu_item( $menu_item ) {
if ( isset( $menu_item->post_type ) ) {
if ( \'nav_menu_item\' == $menu_item->post_type ) {
$menu_item->description = apply_filters( \'nav_menu_description\', $menu_item->post_content );
}
}
return $menu_item;
}
add_filter( \'wp_setup_nav_menu_item\', \'my_plugin_wp_setup_nav_menu_item\' );
// Menu without icons
class my_walker_nav_menu extends Walker_Nav_Menu {
public function display_element($el, &$children, $max_depth, $depth = 0, $args, &$output){
$id = $this->db_fields[\'id\'];
if(isset($children[$el->$id])){
$el->classes[] = \'has_children\';
}
parent::display_element($el, $children, $max_depth, $depth, $args, $output);
}
// add classes to ul sub-menus
function start_lvl( &$output, $depth = 0, $args = array() ) {
// depth dependent classes
$indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent
$display_depth = ( $depth + 1); // because it counts the first submenu as 0
$classes = array(
\'navi\',
( $display_depth ==1 ? \'first\' : \'\' ),
( $display_depth >=2 ? \'navi\' : \'\' ),
\'menu-depth-\' . $display_depth
);
$class_names = implode( \' \', $classes );
// build html
$output .= "\\n" . $indent . \'<ul class="\' . esc_attr($class_names) . \'">\' . "\\n";
}
// add main/sub classes to li\'s and links
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent
static $is_first;
$is_first++;
// depth dependent classes
$depth_classes = array(
( $depth == 0 ? \'main-menu-item\' : \'\' ),
( $depth >=2 ? \'navi\' : \'\' ),
( $is_first ==1 ? \'menu-first\' : \'\' ),
\'menu-item-depth-\' . $depth
);
$depth_class_names = esc_attr( implode( \' \', $depth_classes ) );
// passed classes
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = esc_attr( implode( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) ) );
$is_mega_menu = (strpos($class_names,\'mega\') !== false) ? true : false;
$use_desc = (strpos($class_names,\'use_desc\') !== false) ? true : false;
$no_title = (strpos($class_names,\'no_title\') !== false) ? true : false;
if(!$is_mega_menu){
$class_names .= \' normal_menu_item\';
}
// build html
$output .= $indent . \'<li id="nav-menu-item-\'. esc_attr($item->ID) . \'" class="\' . esc_attr($depth_class_names) . \' \' . esc_attr($class_names) . \'">\';
// link attributes
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
$attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
$attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
$attributes .= ! empty( $item->url ) ? \' href="\' . (($item->url[0] == "#" && !is_front_page()) ? home_url(\'/\') : \'\') . esc_attr($item->url) .\'"\' : \'\';
$attributes .= \' class="menu-link \'.((strpos($item->url,\'#\') === false) ? \'\' : \'scroll\').\' \' . ( $depth > 0 ? \'sub-menu-link\' : \'main-menu-link\' ) . \'"\';
$html_output = ($use_desc) ? \'<div class="description_menu_item">\'.$item->description.\'</div>\' : \'\';
$item_output = (!$no_title) ? \'<a \' . $attributes . \'><span>\' . apply_filters( \'the_title\', $item->title, $item->ID ) . \'</span></a>\'.$html_output : $html_output;
// build html
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args ).(($is_mega_menu)?\'<div class="sf-mega"><div class="sf-mega-inner clearfix">\':\'\');
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = esc_attr( implode( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) ) );
$is_mega_menu = (strpos($class_names,\'mega\') !== false) ? true : false;
$output .= (($is_mega_menu)?\'</div></div>\':\'\') . "</li>\\n";
}
}
锚由以下绳索控制:
$attributes .= ! empty( $item->url ) ? \' href="\' . (($item->url[0] == "#" && !is_front_page()) ? home_url(\'/\') : \'\') . esc_attr($item->url) .\'"\' : \'\';
它甚至说如果url
#
如果不是头版,它应该添加
home_url()
对它。
没有这些,我无法从其他页面滚动到第一页的部分。
为什么要这样做?因为地址是IP而不是www?
ANSWER
显然我没有在我的
wp_nav_menu()
.
感觉很傻现在可以了。很抱歉没有早点检查。