Add The Category and Page Title to Pages As Body Classes

Posted on 21st September, 2015 33 Comments

To add some custom styles to individual category pages you can use this snippet to add a body class to our posts.

functions.php

/* 
 * ADD CATEGORY AND PAGE TITLE TO PAGES AS CLASSES
 *************************************************************/
 
function add_slug_body_class( $classes ) {
	
	// Grab the page title
	global $post;
	if ( isset( $post ) ) {
		$classes[] = $post->post_type . '-' . $post->post_name;
	}
	
	// Grab the category
	if ( is_single() && !is_front_page() && !is_home() )
		return $classes;
	$post_categories = get_the_category();
	foreach( $post_categories as $current_category ) {
		$classes[] =  $current_category->slug; 
	}
	return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

Durekbah

09th, Oct, 20

Beneficial information. With thanks! scholarship essay writing [url=https://discountedessays.com/]writing help[/url] proquest dissertations search

Durekbah

09th, Oct, 20

Wonderful content. Many thanks. best website to get essays [url=https://englishessayhelp.com/]cheap essay writing services[/url] thesis degree

MelviFug

12th, Oct, 20

You made the point.
canadian pharmacy viagra brand [url=https://canada21health.com/]board of pharmacy[/url] meds online

DwayneDethy

12th, Oct, 20

With thanks, I appreciate it. aarp approved canadian online pharmacies [url=https://canadianmsnpharmacy.com/]canada pharmaceuticals online[/url] discount drugs online pharmacy

Durekbah

14th, Oct, 20

Good info. Kudos! help with essays assignments [url=https://paperwritingservicestops.com/]essays writing service[/url] write your dissertation

MelviFug

14th, Oct, 20

Thanks a lot, Excellent stuff!
internet pharmacy [url=https://canadianpharmaciesmsn.com/]canadian online pharmacies[/url] cialis online

Direkbah

14th, Oct, 20

Very good forum posts. Cheers! writing argumentative essays [url=https://theessayswriters.com/]homework hotline[/url] phd dissertation length

DwayneDethy

15th, Oct, 20

Fine content. Appreciate it! canadian pharmacies that are legit [url=https://canadianpharmaceuticalsrx.com/]legal canadian prescription drugs online[/url] londondrugs

MelviFug

16th, Oct, 20

Factor certainly regarded!.
canada pharma limited [url=https://canada21health.com/]northwest pharmacy[/url] canadian pharmacy no prescription

Durekbah

17th, Oct, 20

Fine knowledge. Cheers! how to write an essay for scholarship [url=https://scoringessays.com/]essay writing service[/url] social science dissertation

DwayneDethy

18th, Oct, 20

Superb data. Regards! canada pharmaceuticals [url=https://canadianonlinepharmacyhere.com/]canada drug[/url] canadian pharcharmy online fda approved

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]