wordpress导航菜单的链接是默认在当前页打开的
只要在wp-includes\nav-menu-template.php的start_el函数加上三行代码就OK了
1 /** 2 * @see Walker::start_el() 3 * @since 3.0.0 4 * 5 * @param string $output Passed by reference. Used to append additional content. 6 * @param object $item Menu item data object. 7 * @param int $depth Depth of menu item. Used for padding. 8 * @param int $current_page Menu item ID. 9 * @param object $args 10 */ 11 function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { 12 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ‘‘; 13 14 $class_names = $value = ‘‘; 15 16 $classes = empty( $item->classes ) ? array() : (array) $item->classes; 17 $classes[] = ‘menu-item-‘ . $item->ID; 18 19 $class_names = join( ‘ ‘, apply_filters( ‘nav_menu_css_class‘, array_filter( $classes ), $item, $args ) ); 20 $class_names = $class_names ? ‘ class="‘ . esc_attr( $class_names ) . ‘"‘ : ‘‘; 21 22 $id = apply_filters( ‘nav_menu_item_id‘, ‘menu-item-‘. $item->ID, $item, $args ); 23 $id = $id ? ‘ id="‘ . esc_attr( $id ) . ‘"‘ : ‘‘; 24 25 $output .= $indent . ‘<li‘ . $id . $value . $class_names .‘>‘; 26 27 //此处新增开始 28 if($item->type=="custom") 29 { 30 $item->target="_blank"; 31 } 32 //此处新增结束 33 34 $atts = array(); 35 $atts[‘title‘] = ! empty( $item->attr_title ) ? $item->attr_title : ‘‘; 36 $atts[‘target‘] = ! empty( $item->target ) ? $item->target : ‘‘; 37 $atts[‘rel‘] = ! empty( $item->xfn ) ? $item->xfn : ‘‘; 38 $atts[‘href‘] = ! empty( $item->url ) ? $item->url : ‘‘; 39 40 $atts = apply_filters( ‘nav_menu_link_attributes‘, $atts, $item, $args ); 41 42 $attributes = ‘‘; 43 foreach ( $atts as $attr => $value ) { 44 if ( ! empty( $value ) ) { 45 $value = ( ‘href‘ === $attr ) ? esc_url( $value ) : esc_attr( $value ); 46 $attributes .= ‘ ‘ . $attr . ‘="‘ . $value . ‘"‘; 47 } 48 } 49 50 $item_output = $args->before; 51 $item_output .= ‘<a‘. $attributes .‘>‘; 52 $item_output .= $args->link_before . apply_filters( ‘the_title‘, $item->title, $item->ID ) . $args->link_after; 53 $item_output .= ‘</a>‘; 54 $item_output .= $args->after; 55 56 $output .= apply_filters( ‘walker_nav_menu_start_el‘, $item_output, $item, $depth, $args ); 57 }
时间: 2024-11-06 08:11:35