简体   繁体   English

需要向自定义WordPress导航菜单添加“活动”类

[英]Need to add 'active' class to custom WordPress Navigation Menu

I am creating my own Mega Menu using the in-built navigation menu in WordPress. 我正在使用WordPress中的内置导航菜单创建自己的Mega Menu。 It works great to display different code for a standard menu item and a menu item that has a mega menu. 为标准菜单项和具有大型菜单的菜单项显示不同的代码非常有用。 but I want to be able to reference the active menu item and add the class active into that list item. 但我希望能够引用活动菜单项并将活动类添加到该列表项中。

I am just unsure, with the following code, what I need to put where ? 我只是不确定,用下面的代码,我需要放在哪里?

// if there are items in the primary menu
if ( $items = wp_get_nav_menu_items( $menu->name ) ) {
// loop through all menu items to display their content
foreach ( $items as $item ) {
// if the current item is not a top level item, skip it
if ($item->menu_item_parent != 0) {
    continue;
}

// get the ID of the current nav item
$curNavItemID = $item->ID;
// get the custom classes for the item
// (determined within the WordPress Appearance > Menu section)
$classList = implode(" ", $item->classes);
    echo "<li class=\"nav-item {$classList}\">";                     
if ( in_array('has-mega-menu', $item->classes)) {
    echo "<a class=\"nav-link dropdown-toggle\" href=\"{$item->url}\" id=\"navbarDropdown\" role=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">{$item->title}</a>\n";
}
else  {
    echo "<a class=\"nav-link\" href=\"{$item->url}\">{$item->title}</a>";
}

[EDITED] [编辑]

I know something needs to be added after this line 我知道此行后需要添加一些内容

$classList = implode(" ", $item->classes);
echo "<li class=\"nav-item {$classList}\">";                     

Something to reference the current page 引用当前页面的内容

if ( [ ???? current page or current menu ???? ] 
echo "<li class=\"nav-item active {$classList}\">";

How do I reference the current page? 如何参考当前页面?

Or how do I add the standard WordPress classes to all menu items then I can reference that within the CSS files. 或如何将标准WordPress类添加到所有菜单项,然后可以在CSS文件中引用它。


A Solution 一个解法

I have found a solution. 我找到了解决方案。

I added this 我加了这个

// get the current page URL
$actual_link = ( isset( $_SERVER['HTTPS'] ) ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

before 之前

// get the ID of the current nav item
$curNavItemID = $item->ID;

and then referenced it later by changing this line 然后稍后通过更改此行来引用它

echo "<li class=\"nav-item {$classList}\">";

to this 对此

echo "<li class=\"nav-item ";
if ( $actual_link == $item->url ) {
echo 'active';
}
echo " {$classList}\">";

The 'active' class is now added to any of the menu items who's URL matches the URL of the browser window. 现在,“活动”类已添加到URL与浏览器窗口URL匹配的任何菜单项。 ie: the 'current page'. 即:“当前页面”。

I'm sure there is a more elegant way to do this - but this seems to do what I need. 我敢肯定有一种更优雅的方法可以做到这一点-但这似乎可以满足我的需求。

add this in your css file 将此添加到您的css文件中

 ul.nav-menu li.current-menu-item > a { color: #f7931d !important; } li.current-menu-parent >a, .current-menu-item >a { color: #f7931d !important; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM