Skip to main content
added 38 characters in body
Source Link
GabLeRoux
  • 215
  • 2
  • 13

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

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 ( get_post_type() == "artist" )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    if ( isset($item->url) )
      if ( strstr( $current_url, $item->url) )
        $classes[] = 'current-menu-item';
  }
  
  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 );
}
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

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 ( get_post_type() == "artist" )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    if ( strstr( $current_url, $item->url) )
      $classes[] = 'current-menu-item';
  }
  
  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 );
}
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

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 ( get_post_type() == "artist" )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    if ( isset($item->url) )
      if ( strstr( $current_url, $item->url) )
        $classes[] = 'current-menu-item';
  }
  
  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 );
}
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

added 11 characters in body
Source Link
GabLeRoux
  • 215
  • 2
  • 13

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

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 ( get_post_type() == "artist" && strstr( $current_url, $item->url) )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    if ( strstr( $current_url, $item->url) )
      $classes[] = 'current-menu-item';
  }
  
  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 );
}
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

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 ( get_post_type() == "artist" && strstr( $current_url, $item->url) )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    $classes[] = 'current-menu-item';
  }

  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 );
}

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

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 ( get_post_type() == "artist" )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    if ( strstr( $current_url, $item->url) )
      $classes[] = 'current-menu-item';
  }
  
  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 );
}
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.

Source Link
GabLeRoux
  • 215
  • 2
  • 13

I found an answer by searching in similar posts and links. I added a line to fit my needs (I wanted to prevent my blog page to get highlited when on a custom post).See this line: unset($classes[array_search('current_page_parent',$classes)]);

Solution

add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 3 );

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 ( get_post_type() == "artist" && strstr( $current_url, $item->url) )
  {
    unset($classes[array_search('current_page_parent',$classes)]);
    $classes[] = 'current-menu-item';
  }

  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 );
}

Source

I am not a fan of this answer because it's using the url to compare the element in the menu item, but this one will work fine for different menu IDs and is ok to use in a plugin. Hope it helps someone, lost a couple hours for something I consider a wordpress bug.