所以我目前有Wordpress,上面有一些导航链接:
链接 1 |链接 2 |链接 3 等
两件事,首先我尝试在链接 2 和链接 3 之间添加一个链接。我正在使用此代码,但它将链接添加到无序列表的末尾。有没有办法指定它的位置?
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
$items .= '<li><a href=#">Test Link</a></li>';
return $items;
}
其次,我希望这个新链接有一个包含用户 ID 的动态 URL。我尝试使用wordpress查找有关get_currentuserinfo();
的信息,但我似乎找不到将其包含在函数中的方法。
我在函数.php页面中添加了一个过滤器来wp_nav_menu:
function wp_username_in_nav( $menu ){
global $current_user;
return $menu = str_replace('--username--', $current_user->user_login, $menu );
}
add_filter( 'wp_nav_menu', 'wp_username_in_nav' );
然后在我的菜单中,我添加了一个链接菜单项,其中包含如下 URL:/rootfolder/members/--username--/profile/edit/
这使我能够动态地将用户名添加到菜单项。
-
您可以使用add_filter指定过滤器优先级,尝试使用值
10
,直到您的导航项处于正确的位置。 -
在调用函数之前,您需要设置全局变量
$current_user
。
法典
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "'n";