• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Add Custom Body Class Based on Permalink Slug

This code adds a custom body class when the permalink contains the slug test-slug. If it doesn’t, no custom body class is added.

Could also be written like this :

add_filter( 'body_class', 'add_my_page_body_class' );
function add_my_page_body_class( $classes ) {

	  $ternary = strpos($_SERVER['REQUEST_URI'], 'test-slug' ) ? 'custom-body-class' : '';
	  
	  $classes[] = $ternary;
	  
	  return $classes;
	
}

Or like this :

add_filter( 'body_class', 'add_my_page_body_class' );
function add_my_page_body_class( $classes ) {
	  
	  $classes[] = strpos($_SERVER['REQUEST_URI'], 'test-slug' ) ? 'custom-body-class' : '';
	  
	  return $classes;
	
}

Based on a question from a member of the Genesis community.

This code uses a ternary operator however you could also use a else if statement.

Works in any WordPress theme.

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags