Add active class to Blog & Stream menu links on single posts

Add better site awareness with a subtle touch.

Preview PHP
Click to copy

Paste to your child theme's functions.php:

<?php
/**
 * Make nav menus active on other types of pages.
 *
 * @since 1.0
 */

function md_child_current_menu_item( $classes, $item ) {
	if (
		( $item->title == 'Blog' && ( is_category() || is_singular( 'post' ) ) ) ||
		( $item->title == 'Stream' && ( is_tax( 'stream_categories' ) || is_singular( 'stream' ) ) )
	)
		$classes[] = 'current-menu-item';
	return $classes;
}
add_filter( 'nav_menu_css_class', 'md_current_menu_item', 10, 2 );

Active Blog menu link on single posts

Make “Blog” and other post type archive menu links active when viewing single posts

Even when viewing single blog posts it is still nice to have the “Blog” nav menu link show the active status to give a feel for site hierarchy.

With this snippet you can match the nav link text with a conditional tag to add the <code>current-menu-item</code> class to a specific text string.

This code example targets a nav menu link labeled “Blog” and also highlights it when you are viewing category pages and single blog posts. You can also add more conditionals to target other text links like I have with the Stream.

Start Building Websites That Delight

You can use the Marketers Delight WordPress Theme on unlimited sites. New features & updates are delivered to sites with your active license key.

Add to Cart
Comments (0)Forum replies (0)

No replies yet

Loading new replies...

Alex

MD developer

5,527 messages 1,444 likes

Even when viewing single blog posts it is still nice to have the "Blog" nav menu link show the active status to give a feel for site hierarchy.

With this snippet you can match the nav link text with a conditional tag to add the current-menu-item class to a specific text string.

The example below targets a nav menu link labeled "Blog" and also highlights it when you are viewing category pages and single blog posts. You can also add more conditionals to target other text links like I have with the Stream.

Paste to your functions.php file.

<?php
/**
 * Make nav menus active on other types of pages.
 *
 * @since 1.0
 */

function md_child_current_menu_item( $classes, $item ) {
    if (
        ( $item->title == 'Blog' && ( is_category() || is_singular( 'post' ) ) ) ||
        ( $item->title == 'Stream' && ( is_tax( 'stream_categories' ) || is_singular( 'stream' ) ) )
    )
        $classes[] = 'current-menu-item';
    return $classes;
}
add_filter( 'nav_menu_css_class', 'md_current_menu_item', 10, 2 );

Reply Like

click to expand...
Alex

MD developer

5,334 messages 1,333 likes

Even when viewing single blog posts it is still nice to have the "Blog" nav menu link show the active status to give a feel for site hierarchy.

With this snippet you can match the nav link text with a conditional tag to add the current-menu-item class to a specific text string.

The example below targets a nav menu link labeled "Blog" and also highlights it when you are viewing category pages and single blog posts. You can also add more conditionals to target other text links like I have with the Stream.

Paste to your functions.php file.

<?php
/**
 * Make nav menus active on other types of pages.
 *
 * @since 1.0
 */

function md_child_current_menu_item( $classes, $item ) {
    if (
        ( $item->title == 'Blog' && ( is_category() || is_singular( 'post' ) ) ) ||
        ( $item->title == 'Stream' && ( is_tax( 'stream_categories' ) || is_singular( 'stream' ) ) )
    )
        $classes[] = 'current-menu-item';
    return $classes;
}
add_filter( 'nav_menu_css_class', 'md_current_menu_item', 10, 2 );

Reply Like

click to expand...

Leave a Comment

Start building your website with MD now

Get started with MD today. Pay once for use on unlimited websites.
* Save 40% on license key renewals every 12 months. Get auto-updates sent to 5 websites.

New: Get Drop-ins faster from your newly redesigned account area →

Labs

Get the latest MD sent straight to your inbox

There's always something new in the MD Labs! Join the email list to get the newest content and releases sent straight to your inbox.*

Join the MD Newsletter

* I only email you when there's something new, or to share an exciting MD sneak peak.