Add The Category and Page Title to Pages As Body Classes

Posted on 21st September, 2015 185 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

JamesVen

21st, Oct, 20

how unreal it can be to do your pasty diagnostics [url=https://ciamedusa.com/#]cheapest cialis[/url] of complete from your serene to liberal the introductory amount.

Durekbah

05th, Nov, 20

Appreciate it. Plenty of data.
compare and contrast college essay [url=https://writingthesistops.com/]dissertations[/url] uk dissertation help

Direkbah

06th, Nov, 20

You made your position quite nicely!. writing a research essay [url=https://topswritingservices.com/]write my essay[/url] dissertation

viagra online prices

07th, Nov, 20

While the program will require a little bit of preparation to enter, you will earn a complete and
fast paced education that will prepare you to move up in your chosen field.
viagra online prices buy viagra online
without prescription

Durekbah

08th, Nov, 20

Wow quite a lot of fantastic data! type of essays writing [url=https://englishessayhelp.com/]help in thesis writing[/url] phd thesis cover page

Direkbah

09th, Nov, 20

Useful tips. Kudos. how to write a self evaluation essay [url=https://essayextra.com/]essay typer[/url] thesis or dissertation

Durekbah

10th, Nov, 20

Thanks a lot. Wonderful stuff! how to write an amazing essay [url=https://discountedessays.com/]write paper[/url] writing a good dissertation

Direkbah

11th, Nov, 20

Whoa all kinds of excellent information. how to write an essay essay [url=https://scoringessays.com/]couseworks[/url] writing a dissertation

Ac repair

12th, Nov, 20

Normally I don’t read post on blogs, but I wish to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, very nice post.

Durekbah

13th, Nov, 20

You actually said that really well! college essay music [url=https://topessayssites.com/]writing dissertation[/url] how to write dissertation

BrianTat

17th, Nov, 20

Wonderful posts. Many thanks.

How Long For Provigil To Work [url=https://topmodafinilrxpills.com/]modafinil side effects[/url]

Deidra Gandrud

19th, Nov, 20

I used to be more than happy to search out this web-site.I wished to thanks for your time for this wonderful learn!! I undoubtedly enjoying every little little bit of it and I’ve you bookmarked to take a look at new stuff you blog post.

Durekbah

19th, Nov, 20

You said it very well.! issues to write about in a college essay [url=https://discountedessays.com/]write papers[/url] the essay

you can try here electrician

02nd, Dec, 20

Just wish to say your article is as surprising. The clearness in your post is just excellent and i can assume you’re an expert on this subject. Fine with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the enjoyable work.

important site electrical repair san diego

03rd, Dec, 20

We’re a group of volunteers and opening a new scheme in our community. Your website provided us with useful information to paintings on. You have done a formidable task and our whole community will likely be grateful to you.

hey

05th, Jan, 21

Taxi moto line
128 Rue la Boétie
75008 Paris
+33 6 51 612 712  

Taxi moto paris

Attractive portion of content. I just stumbled upon your blog and in accession capital to assert that I acquire in fact loved account your weblog posts.
Anyway I will be subscribing on your feeds or even I
success you get entry to consistently rapidly.

Bennett Buonaiuto

08th, Jan, 21

I live in Costa Rica and am about to publish a community newspaper, what are the steps to go about copyrighting the name and articles published in it? And also when can one start using the copyright symbol?.

vreyrolinomit

09th, Jan, 21

I like what you guys are up too. Such smart work and reporting! Carry on the superb works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my website :).

phishing

10th, Jan, 21

Hi, I do believe this is a great blog. I stumbledupon it 😉 I may return once again since i have book marked it.
Money and freedom is the greatest way to change, may you be rich and continue to guide
others.

vreyrolinomit

19th, Jan, 21

A person necessarily help to make severely articles I might state. That is the first time I frequented your web page and to this point? I surprised with the research you made to create this particular publish incredible. Wonderful process!

Lcxeloks

19th, Jan, 21

where to buy viagra singapore viagra syd viagra\\\’ [url=http://genqpviag.com/]which one is better viagra cialis or laverta[/url] ’

WilsonSetle

20th, Jan, 21

chloroquine amazon
[url=https://chloroquineorigin.com/#]aralen for sale[/url]
when erectile dysfunction meds quite working
[url=https://erectiledysfunctionbjul.com/#]can erectile dysfunction be prevented[/url]
hydroxyquine drug
[url=https://hydroxychloroquinex.com/#]azathioprine[/url]
erectile potion
[url=https://plaquenilx.com/#]plaquenil classification[/url]
elevex tadalafil 20mg
[url=https://tadalisxs.com/#]tadalafil generic[/url]
zithromax medication
[url=https://zithromaxes.com/#]zithromax antibiotic without prescription[/url]

viagra pro nfjnzo

23rd, Jan, 21

viagra prescription online [url=http://www.canada1drugstore.com/tadalafil/]over the counter viagra[/url] natural alternative to viagra
canada meds natural substitute for viagra

zortilo nrel

25th, Jan, 21

F*ckin’ awesome things here. I’m very glad to see your post. Thanks a lot and i am looking forward to contact you. Will you kindly drop me a mail?

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]