Front-End & Daily

How to add the "current" class to navigation on custom post type pages

How to add the

What I want to do

When setting up navigation with wp_nav_menu in WordPress,

wp_nav_menu( array('menu' => 'header') );

I want to add "current" to the navigation when a custom post type detail page is open.

How to add the

How to do it

How to create navigation

First, let's check how to create navigation.

If you have a custom post type called "Event Information" and want to add it to the navigation,

Add it to the menu from the "Event Information List" item.

Edit WordPress Menu Post Type Archive

When has_archive is false, "Event Information List" will not be displayed here, so please add it using "Pages" or "Custom Links."

In that case, please refer to the latter for the description to add to functions.php.

Add to functions.php

Add the following to functions.php.

// Add current to navigation on custom post type pages
function nav_current($classes, $item){

	// Add current to navigation on custom post type pages
	if($item -> type == 'post_type_archive'){
		$post_tyle = $item -> object;
		if(is_singular($post_tyle)){
			$classes[] = 'current-menu-item';
		}
	}

	// Add current to navigation on regular post pages
	if($item -> title == 'News'){ // Change "News" as appropriate
		if(is_singular('post')){
			$classes[] = 'current-menu-item';
		}
	}

	return $classes;
} 
add_filter( 'nav_menu_css_class', 'nav_current', 10, 2 );

Let's say there is a custom post type with the slug "event."

Looking at the navigation li tags in order,

In line 6, $item -> object will contain "event"
In line 7, when the "event" detail page is open,
In line 8, the "current-menu-item" class is added to this li tag.
This is how it works.

Even if there are multiple custom post types, this single code can handle them all!

The latter part is the description for adding "current" to regular post pages.

Please change "News" in line 13 as appropriate to match the display name of the li tag.

In line 14, when "post" is open, the "current-menu-item" class is added.
This is how it works.

If successful, "current" should be added!

How to add the

When "has_archive" is false

When has_archive is false,

I think you'll have to create the navigation using "Custom Links" or similar.

Edit WordPress Menu Custom Link

In this case, add the following to functions.php.

// Add current to navigation on custom post type pages
function nav_current($classes, $item){

	// Add current to navigation on custom post type pages
	if($item -> title == 'Event'){ // If the display name of the li tag is "Event"
		if(is_singular('event')){ // When the event post page is open
			$classes[] = 'current-menu-item';
		}
	}

	// Add current to navigation on custom post type pages
	if($item -> title == 'Work History'){ // If the display name of the li tag is "Work History"
		if(is_singular('works')){ // When the works post page is open
			$classes[] = 'current-menu-item';
		}
	}

	// Add current to navigation on regular post pages
	if($item -> title == 'News'){ // Change "News" as appropriate
		if(is_singular('post')){
			$classes[] = 'current-menu-item';
		}
	}
	return $classes;
} 
add_filter( 'nav_menu_css_class', 'nav_current', 10, 2 );

This is an example when there are two custom post types.

Event (event)

Work History (works)

You will need to duplicate the if statement each time a custom post type is added.

Comments

If you found something different in your environment, please feel free to comment.

After reviewing the content, we will publish it, omitting personal information.

Enter your name and email address

Please enter if you would like a reply via email.

The personal information provided will not be disclosed. It will only be used for replies.

It will be sent directly. Please confirm and click "Send."

If this was helpful, we appreciate your support!
Any support received will be used for childcare.

OFUSEで応援を送る


Or support us by buying something from the buttons below
(You don't have to buy the linked product.)

Amazon

楽天市場

Yahoo!ショッピング

PR

As an Amazon Associate, "Ken" earns from qualifying purchases.

Share

Share on Twitter Share on Facebook Share on LINE Share on Hatena Bookmark