admin管理员组文章数量:1022997
I have created an event custom post type and a link in my menu to the archive page for that event (/events).
I'm using wp_nav_menu()
to call the menu.
When I'm on /events the menu has the current_menu_item
class and I've styled that to change colors. This works perfectly.
I'd like to enhance this a bit though by setting that menu item to active whenever anyone is on a certain page as well.
I created a page to show my past events (/past-events). It's being driven by a file called page-past-events.php
. The idea is that when a user is on /past-events
the events link shows as active. Since I don't have a menu item for that I want the event
menu item to have the active class. Ideally this item would be active when a user is on any of the single events as well (events/event-12).
my current code is
wp_nav_menu(array(
'theme_location' => 'headerMenu',
));
I found this code for my functions.php:
function my_special_nav_class( $classes, $item ) {
if ( is_page('past-events') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
But it doesn't target my events menu item.
I can do it with script like this:
<?php if (is_page('past-events')) { ?>
<script type="text/javascript">
var d = document.getElementById("menu-item-15");
d.className += " current-menu-item";
</script>
<?php } ?>
But I was hoping for a "WordPress" way.
EDIT
/events
a custom link in the wp menu that links to the events archive.
The slug for events is events
/past-events
is a single page with its own template page-past-events.php
So if someone is on the past events page the menu item for /events gets the active class and for all intents and purposes is highlighted as active.
I have created an event custom post type and a link in my menu to the archive page for that event (/events).
I'm using wp_nav_menu()
to call the menu.
When I'm on /events the menu has the current_menu_item
class and I've styled that to change colors. This works perfectly.
I'd like to enhance this a bit though by setting that menu item to active whenever anyone is on a certain page as well.
I created a page to show my past events (/past-events). It's being driven by a file called page-past-events.php
. The idea is that when a user is on /past-events
the events link shows as active. Since I don't have a menu item for that I want the event
menu item to have the active class. Ideally this item would be active when a user is on any of the single events as well (events/event-12).
my current code is
wp_nav_menu(array(
'theme_location' => 'headerMenu',
));
I found this code for my functions.php:
function my_special_nav_class( $classes, $item ) {
if ( is_page('past-events') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
But it doesn't target my events menu item.
I can do it with script like this:
<?php if (is_page('past-events')) { ?>
<script type="text/javascript">
var d = document.getElementById("menu-item-15");
d.className += " current-menu-item";
</script>
<?php } ?>
But I was hoping for a "WordPress" way.
EDIT
/events
a custom link in the wp menu that links to the events archive.
The slug for events is events
/past-events
is a single page with its own template page-past-events.php
So if someone is on the past events page the menu item for /events gets the active class and for all intents and purposes is highlighted as active.
Share Improve this question edited Apr 17, 2019 at 14:10 rudtek asked Apr 16, 2019 at 21:40 rudtekrudtek 6,3835 gold badges30 silver badges52 bronze badges1 Answer
Reset to default 1If I understand it properly, this would do what you wanted:
function my_special_nav_class( $classes, $item ) {
if (
// 1. The menu item is for the "/events" page. Be sure to change the "123"
// (i.e. the "/events" page ID).
( 'page' === $item->object && 123 == $item->object_id ) &&
// 2. The current page is "Past Events" or that it's a "Event" single page.
// If "event" isn't the correct post type, change it.
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
UPDATE
So if the /events
menu item is a custom link:
function my_special_nav_class( $classes, $item ) {
if (
( 'custom' === $item->type && 'events' === $item->post_name ) &&
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
If that does not work, then try checking against the menu item ID (here it's 456
):
function my_special_nav_class( $classes, $item ) {
if (
456 == $item->ID &&
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
I have created an event custom post type and a link in my menu to the archive page for that event (/events).
I'm using wp_nav_menu()
to call the menu.
When I'm on /events the menu has the current_menu_item
class and I've styled that to change colors. This works perfectly.
I'd like to enhance this a bit though by setting that menu item to active whenever anyone is on a certain page as well.
I created a page to show my past events (/past-events). It's being driven by a file called page-past-events.php
. The idea is that when a user is on /past-events
the events link shows as active. Since I don't have a menu item for that I want the event
menu item to have the active class. Ideally this item would be active when a user is on any of the single events as well (events/event-12).
my current code is
wp_nav_menu(array(
'theme_location' => 'headerMenu',
));
I found this code for my functions.php:
function my_special_nav_class( $classes, $item ) {
if ( is_page('past-events') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
But it doesn't target my events menu item.
I can do it with script like this:
<?php if (is_page('past-events')) { ?>
<script type="text/javascript">
var d = document.getElementById("menu-item-15");
d.className += " current-menu-item";
</script>
<?php } ?>
But I was hoping for a "WordPress" way.
EDIT
/events
a custom link in the wp menu that links to the events archive.
The slug for events is events
/past-events
is a single page with its own template page-past-events.php
So if someone is on the past events page the menu item for /events gets the active class and for all intents and purposes is highlighted as active.
I have created an event custom post type and a link in my menu to the archive page for that event (/events).
I'm using wp_nav_menu()
to call the menu.
When I'm on /events the menu has the current_menu_item
class and I've styled that to change colors. This works perfectly.
I'd like to enhance this a bit though by setting that menu item to active whenever anyone is on a certain page as well.
I created a page to show my past events (/past-events). It's being driven by a file called page-past-events.php
. The idea is that when a user is on /past-events
the events link shows as active. Since I don't have a menu item for that I want the event
menu item to have the active class. Ideally this item would be active when a user is on any of the single events as well (events/event-12).
my current code is
wp_nav_menu(array(
'theme_location' => 'headerMenu',
));
I found this code for my functions.php:
function my_special_nav_class( $classes, $item ) {
if ( is_page('past-events') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
But it doesn't target my events menu item.
I can do it with script like this:
<?php if (is_page('past-events')) { ?>
<script type="text/javascript">
var d = document.getElementById("menu-item-15");
d.className += " current-menu-item";
</script>
<?php } ?>
But I was hoping for a "WordPress" way.
EDIT
/events
a custom link in the wp menu that links to the events archive.
The slug for events is events
/past-events
is a single page with its own template page-past-events.php
So if someone is on the past events page the menu item for /events gets the active class and for all intents and purposes is highlighted as active.
Share Improve this question edited Apr 17, 2019 at 14:10 rudtek asked Apr 16, 2019 at 21:40 rudtekrudtek 6,3835 gold badges30 silver badges52 bronze badges1 Answer
Reset to default 1If I understand it properly, this would do what you wanted:
function my_special_nav_class( $classes, $item ) {
if (
// 1. The menu item is for the "/events" page. Be sure to change the "123"
// (i.e. the "/events" page ID).
( 'page' === $item->object && 123 == $item->object_id ) &&
// 2. The current page is "Past Events" or that it's a "Event" single page.
// If "event" isn't the correct post type, change it.
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
UPDATE
So if the /events
menu item is a custom link:
function my_special_nav_class( $classes, $item ) {
if (
( 'custom' === $item->type && 'events' === $item->post_name ) &&
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
If that does not work, then try checking against the menu item ID (here it's 456
):
function my_special_nav_class( $classes, $item ) {
if (
456 == $item->ID &&
( is_page( 'past-events' ) || is_singular( 'event' ) )
) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
本文标签: menusset certain item in nav walker to active when on archive pages or singles
版权声明:本文标题:menus - set certain item in nav walker to active when on archive pages or singles 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745578927a2157192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论