Controlling file and content includes with body classes in WordPress

Posted on 5th February, 2015 375 Comments

I use this a lot, using a body class to insert stuff is awesome. Whether I’m using it to insert some HTML, control some CSS or including a relative file to a existing page.
This also helps when having sites with multiple plugins can effect the performance so by simply adding a body class to include the file is my favorite way of doing it.

header.php

in the header.php find the body tag and include a call to the classes.

<body <?php body_class( $class ); ?>>

Then above our stylesheets add this. I’m adding it above the stylesheets because I want to potentially use this to include plugin stylesheets.

<?php $classes = get_body_class(); // INCLUDE THE BODY CLASSES ?>

Any other page template file

Include a function to add our body classes, I like to add this at the very top of each template file.

<?php 

## add a custom body class 
add_action( 'body_class', 'add_my_bodyclass'); 
function add_my_bodyclass( $classes ) { 
	$classes[] = 'notitle'; 
	$classes[] = 'popup'; /* EACH NEW CLASS REQUIRES IT'S OWN LINE */
	return $classes; 
}
?>

header.php or footer.php

Next you’ll need to check for the body class to include whatever file that’s needed in relation to that page. So for this i’m going to stick to the most simple example and include javascript in the footer of the theme.

<!-- CONDITIONAL SCRIPT CALLS -->	
				
<?php	if (in_array('popup',$classes)) { ?>
	<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/javascript/jquery.popup.min.js"></script>
<?php };?>

It really is as simple as that. The snip above checks through the list of $classes added to the current page and if that class exists then it’ll include this file.

STYLE.CSS

We can also use this to add conditional individual styles to our stylesheet. So say we have pages where a title isn’t necessary. Using the example above “notitle” is added.
I can then use this in the css like to remove the post title.

.notitle h1 {
	display: none;
}

Hope you’ve found this post useful.

Enjoy.

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]

JamesVen

21st, Oct, 20

Bwnvkg pjnead cialis drugs buy cialis generic online [url=https://ciamedusa.com/]cialis[/url] find out that numerous of these lead array reducing symptoms.

JamesVen

21st, Oct, 20

quantity it will transition the disagreement’s being with no other. [url=https://ciamedusa.com/]ciamedusa.com[/url] Rsrkkj eyexed cialis drug tadalafil 5 mg

JamesVen

22nd, Oct, 20

Motley not crap close to mouth and every few. [url=https://ciamedusa.com/#]buy cialis no prescription[/url] in error pulmonary that you clothed a very severe using.

JamesVen

28th, Oct, 20

a maximum peril or are oblivious any superficial of mi. [url=https://ciamedusa.com/#]cialis dosage[/url] out bladder, which can j psychiatry, bar forebode, and palpitations.

Kennethedugs

05th, Nov, 20

viagra walgreens viagra buy real viagra online
legal to buy prescription drugs from canada

RonaldKer

11th, Nov, 20

cialis daily cialis daily use cialis cost
cialis and interaction with ibutinib

DavidBat

12th, Nov, 20

[url=http://metformintop.com/#]metformin 1000[/url] – 60000 mg metformin
[url=https://zithromax10.com/#]can you buy zithromax over the counter[/url] – generic zithromax medicine
[url=http://diflucanfavdr.com/#]diflucan singapore pharmacy[/url] – can you buy diflucan over the counter in canada

온라인카지노

24th, Nov, 20

Hello, There’s no doubt that your website
유그
ould be having internet browser compatibility problems. Whenever I look at your website in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I just wanted to provide you with a quick heads up! Apart from that, fantastic site!

yo

21st, Dec, 20

continuously i used to read smaller articles or reviews which as well clear their motive,
and that is also happening with this paragraph which I am
reading now.

ดูหนัง 4k

21st, Dec, 20

I really like what you guys are up too. This kind of clever work and exposure!

Keep up the amazing works guys I’ve incorporated you guys to our blogroll.

sbo

22nd, Dec, 20

Excellent web site you have here.. It’s hard to find
good quality writing like yours these days.
I really appreciate individuals like you! Take care!!

hey

22nd, Dec, 20

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

Taxi moto paris

I like the helpful info you provide in your articles.

I will bookmark your weblog and check again here frequently.
I am quite sure I’ll learn lots of new stuff right here!
Best of luck for the next!

JasonMaR

15th, Jan, 21

where can i buy viagra over the counter hslrqzfp viagra viagra without a doctor prescription

KbbtBoth

26th, Jan, 21

cialis canada online Norvasc do genric cialis work [url=https://xz-pharmacyonline.com]Epivir-HBV[/url] ’

FhnhUnobe

29th, Jan, 21

Cialis Professional Imuran canadian pharmacy no prescription [url=https://canadianpharmacy-yy.com/]buy online prescription drugs[/url] ’

cheap flights

31st, Jan, 21

Quality articles is the key to interest the people to pay a
visit the web site, that’s what this website is providing.

cheap flights

01st, Feb, 21

Hi this is kind of of off topic but I was wondering if blogs use WYSIWYG
editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding knowledge so I wanted to get guidance
from someone with experience. Any help would
be enormously appreciated!

Fmnfeloks

01st, Feb, 21

rx online cialis cialis uk viagra and cialis [url=https://impotencecdny.com/info/history.php]cialis online pharmacy australian[/url] ’

cheap flights

01st, Feb, 21

Good day! I could have sworn I’ve visited your blog before but after looking at a few of the posts I realized it’s new
to me. Anyhow, I’m certainly pleased I found
it and I’ll be book-marking it and checking back frequently!

tempe roadside service

04th, Feb, 21

I know this if off topic but I’m looking into starting my own blog and was wondering
what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web savvy so I’m not 100% certain. Any recommendations
or advice would be greatly appreciated. Many thanks

tadalafil online pharmacy

20th, Feb, 21

Good day I am so glad I found your webpage, I really found you by accident, while I was looking on Aol for something else, Anyways I am here now and would just
like to say kudos for a incredible post and a all round entertaining blog (I also
love the theme/design), I don’t have time to browse it all
at the minute but I have bookmarked it and also added
in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb job. https://cialis.webbfenix.com/tadalafil

Youtube Video Download

07th, Apr, 21

Nice Article to download YouTube videos by using https://ytzip.com Open ytzip.com Youtube video download website and enter the video’s URL to the corresponding field, at the top of the page. Click “Download” and youtube video download will begin.

cialis online canada

17th, Apr, 21

Have you ever thought about including a little bit more than just your articles?
I mean, what you say is fundamental and all.

However think about if you added some great pictures or videos to give your posts more, “pop”!

Your content is excellent but with images and
videos, this website could definitely be one of the best in its niche.
Amazing blog! https://cialis.advantagetriseal.com/tadalafil

tadalafil tablets in india

20th, Apr, 21

I do not know if it’s just me or if everyone else experiencing
problems with your site. It appears as though some of the written text in your posts are running
off the screen. Can someone else please comment and let me
know if this is happening to them as well?
This might be a issue with my internet browser because I’ve had this happen before.
Many thanks https://cialis.cleckleyfloors.com/tadalafil

viagra barata madrid

04th, Jun, 21

viagra cialis prix [url=https://franceviag.com/#]viagra homme sans ordonnance [/url] site fiable viagra en ligne avis cialis ou viagra

JosephWex

27th, Jun, 21

cheap flights tickets visit website really dirt cheap flight tickets [url=https://cheapticketsairlineworldwide.com/#]click here[/url] best airline credit cards

metronidazole dental

08th, Jul, 21

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using Movable-type on various websites
for about a year and am concerned about switching to another platform.
I have heard fantastic things about blogengine.net. Is there a
way I can import all my wordpress content into it?
Any kind of help would be greatly appreciated! http://antiibioticsland.com/Flagyl_ER.htm

Google

23rd, Jul, 21

Google

Check below, are some totally unrelated internet websites to ours, however, they may be most trustworthy sources that we use.

covid-19 made in thailand

15th, Sep, 21

Thank you for the good writeup. It actually was a
entertainment account it. Look complex to far brought agreeable
from you! By the way, how could we keep in touch?

EdwardSen

23rd, Sep, 21

Ton Ton
blumb
Esoma
[url=https://cdn.thingiverse.com/assets/84/79/14/66/84/kurokos_basketball_season_2_tagalog_version_full_11.html]thingiverse.com[/url]
CiniamambTon

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]