Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:09:39+00:00 2026-06-06T23:09:39+00:00

I have a wordpress theme using wp_nav_menu() to build the menu. I have a

  • 0

I have a wordpress theme using wp_nav_menu() to build the menu. I have a custom menu item in this menu that goes to a non-standard wordpress page. Is there a way I can conditionally set this nav item to current based on a match in the url string?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:09:41+00:00Added an answer on June 6, 2026 at 11:09 pm

    You can use the nav_menu_css_class in WordPress to achieve the result you’re after. Using this hook you can alter an array of CSS classes that can be applied to the menu.

    add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 2 );
    
    function add_parent_url_menu_class( $classes = array(), $item = false ) {
        // Get current URL
        $current_url = current_url();
        // Get homepage URL
        $homepage_url = trailingslashit( get_bloginfo( 'url' ) );
        // Exclude 404 and homepage
        if( is_404() or $item->url == $homepage_url ) return $classes;
            if ( strstr( $current_url, $item->url) ) {
                // Add the 'parent_url' class
                $classes[] = 'parent_url';
            }
        return $classes;
    }
    
    function current_url() {
        // Protocol
        $url = ( 'on' == $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
        $url .= $_SERVER['SERVER_NAME'];
        // Port
        $url .= ( '80' == $_SERVER['SERVER_PORT'] ) ? '' : ':' . $_SERVER['SERVER_PORT'];
        $url .= $_SERVER['REQUEST_URI'];
    
        return trailingslashit( $url );
    }
    

    This method will ignore 404 pages or the root of the site, but will add the class to the menu item if the current URL matches the menu item URL.

    Full credit for this code: http://www.rarescosma.com/2010/11/add-a-class-to-wp_nav_menu-items-with-urls-included-in-the-current-url/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the WordPress theme Studio8 and I have create some custom forms
I have a wordpress theme that has a build in shortcode for creating an
this is function.php of a wordpress theme that NOD32 says this code have a
I have been working on a custom wordpress theme, this is part of a
I have a Wordpress site (www.nurseresumetips.com) and I'm using a theme named JVPress. This
I have a wordpress theme that I like to duplicate. To make things easier
I have a Wordpress theme that uses MooTools and jQuery. I use jQuery noConflict
I am using a custom post type in my wordpress theme, and I need
I'm programming a website using Foundation theme for wordpress that is setup for responsive
I have installed a Wordpress theme in /wp-content/themes/mytheme/ I then applied this theme to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.