• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Add Custom Body Class in Genesis

Last Updated on November 19, 2018 Favorited: 0 times

Body class in WordPress plays an important role while designing a site. It helps to target a specific page, post or a similar post, page type using WordPress conditional tags.

Most of the time, default output body classes work to customize the elements but sometimes it is important to output a specific class to target those pages or content on a specific page.

Body class is important and can be added on any page including default post and page type using Conditional Tags in WordPress.

1. GLOBAL

Here is the default snippet to output some class globally i.e my-class-name. Use this in functions.php

add_filter( 'body_class', 'prefix_custom_body_class' );
/**
 * Adds a css class to the body element.
 *
 * @param  array $classes the current body classes.
 * @return array $classes modified classes.
 */
function prefix_custom_body_class( $classes ) {

    $classes[] = 'my-class-name';
    return $classes;
}

2. Conditional

Now, the snippet when we target a page which is a part of Conditional Tags. Use this in functions.php for a specific page name sample-page.

add_filter( 'body_class', 'prefix_custom_body_class' );
/**
 * Adds a css class to the body element.
 *
 * @param  array $classes the current body classes.
 * @return array $classes modified classes.
 */
function prefix_custom_body_class( $classes ) {
	
	//* use conditional tag @https://codex.wordpress.org/Conditional_Tags
	if ( is_page( 'sample-page' ) ) //* multiple tags → is_singular( array ('post','page') )

    $classes[] = 'custom-page-class';
    return $classes;
}

3. Multiple Condition

We can use array() to target multiple pages with the help of WordPress Conditional Tags. Like we can use this in functions.php

//* Add custom body class to the head
add_filter( 'body_class', 'gk_some_body_class' );
function gk_some_body_class( $classes ) {
	
	if ( is_singular( array ('post','page') )  )
		$classes[] = 'some-class';
		return $classes;
}

Similarly, One can target archive page using is_archive(), category using is_category() or a CPT archive using is_post_type_archive( ‘portfolio’ ) in functions.php using the above snippet.

Remember: We can include the global snippet in a template to add the custom body class without using a conditional tag as the template itself work as conditional for a specific page.

Related Posts

  • Conditional Tag is_front_page() v/s is_home(). What to use?
  • Conditional site footer in Genesis
  • Replace site footer widget area conditionally in Genesis
  • Customize Post Meta conditionally in Genesis
  • Customize Post info conditionally in Genesis

Categories: Free Content, Genesis Tutorials Tags: body class, conditional tag, conditional tags

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service