Skip to content

Instantly share code, notes, and snippets.

@fugudesign
Last active November 15, 2016 13:23
Show Gist options
  • Save fugudesign/ba5145d87e14b08a6290a70c3ac1b95a to your computer and use it in GitHub Desktop.
Save fugudesign/ba5145d87e14b08a6290a70c3ac1b95a to your computer and use it in GitHub Desktop.
Wordpress functions for all blog pages (body classes and menu items)
/**
* Define if the current page is a blog page or not
*
* @global $post
* @return bool true if current page is a blog page
*/
function is_blog () {
global $post;
return ( ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag() ) && get_post_type( $post ) == 'post' );
}
/**
* Adds a class group-blog to all blog pages
*
* @param array $classes Classes for the body element.
* @return array $classes
*/
function my_body_classes( $classes ) {
if ( is_blog() ) {
$classes[] = 'group-blog';
}
return $classes;
}
add_filter( 'body_class', 'my_body_classes' );
/**
* Highlight all blog pages in menus
*
* @param array $classes Classes for the menu item
* @param bool|obj $menu_item The menu item
* @return array $classes
*/
function my_menu_item_custom_class( $classes=array(), $menu_item=false ) {
$page_for_posts = get_option( 'page_for_posts' );
if ( is_blog() && $menu_item->object_id == $page_for_posts && !in_array( 'current-menu-item', $classes ) ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter('nav_menu_css_class', 'my_menu_item_custom_class', 100, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment