我需要显示如下简单菜单:
<nav class="main">
<a href="/" class="home">
<span class="logo"></span>
<span class="company-name">My Company</span>
<span class="page-name">Home</span>
</a>
<a href="one-page">One page</a>
<a href="two-page" class="selected-current">Two page</a>
<a href="three-page">Three page</a>
<a href="about">About</a>
<div class="account">
<a href="login" class="login">Login</a>
</div>
为此,我采取以下方式:
/主题/功能。php
<?php
// Adding Log in/out links to a Specific WordPress Menu
add_filter( \'wp_nav_menu_items\', \'add_loginout_link\', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == \'primary\') {
$items .= \'<div class="account"><a href="\'. wp_logout_url() .\'" class="login">Logout</a></div>\';
}
elseif (!is_user_logged_in() && $args->theme_location == \'primary\') {
$items .= \'<div class="account"><a href="\'. site_url(\'wp-login.php\') .\'" class="login">Login</a></div>\';
}
return $items;
}
/主题/标题。php
<nav class="main"><?php
$menuParameters = array(
\'theme_location\' => \'primary\',
\'fallback_cb\' => \'starkers_menu\',
\'container\' => false,
\'echo\' => false,
\'items_wrap\' => \'%3$s\',
\'depth\' => 0,
);
echo strip_tags(wp_nav_menu( $menuParameters ), \'<a><div>\' );
?></nav>
我无法管理当前的类页面和第一个url(主页)的标记(span)。
提前感谢您的帮助。
诚恳地
SO网友:Fabien Quatravaux
要实现这一点,您必须扩展Walker_Nav_Menu 班
首先,创建一个扩展Walker\\u Nav\\u菜单的类
/**
* Custom nav menu.
* Code copy-pasted from /wp-includes/nav-menu-template.php
*
* @package WordPress
* @since 3.0.0
* @uses Walker_Nav_Menu
*/
class User16975_Custom_Menu_Walker extends Walker_Nav_Menu {
/**
* @see Walker_Nav_Menu::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference.
*/
function start_lvl(&$output) {}
/**
* @see Walker_Nav_Menu::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference.
*/
function end_lvl(&$output) {
}
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
}
}
然后,实施
start_el
根据您的需要发挥作用。
最后,让WP使用您的类而不是默认类:
add_filter( \'wp_edit_nav_menu_walker\', \'user16975_walker_class\');
function user16975_walker_class($class){
return "User16975_Custom_Menu_Walker";
}
您还可以实现
end_lvl
在类中添加登录链接。