Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Removes the `current_page_parent` class from the blog page link when not viewing a blog related page
<?php
/**
* Removes the `current_page_parent` class from the blog page link when not viewing a blog related page
*
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
function cameronjonesweb_remove_current_page_parent( $classes, $item, $args, $depth ) {
// Fix the current_page_parent class being added to custom post type archives.
if ( in_array( 'current_page_parent', $classes, true ) ) {
$blog_page = get_option( 'page_for_posts' );
if ( ! is_home() && ! is_singular( 'post' ) && ! is_category() && ! is_tag() && ! is_author() && wp_get_post_parent_id( $item->object_id ) !== $blog_page ) {
$key = array_search( 'current_page_parent', $classes, true );
unset( $classes[ $key ] );
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.