WordPress Body Class 101: Tips and Tricks for Theme Designers

[agentsw ua=’pc’]

Are you an aspiring WordPress theme designer looking for new ways to use CSS into your themes?

Luckily, WordPress automatically adds CSS classes that you can utilize in your themes. Several of these CSS classes are automatically added to the <body> section of every page on a WordPress site.

In this article, we will explain the WordPress body class with tips and tricks for aspiring theme designers to utilize them in their projects.

wpbodyclass
Here is a quick overview of what you’ll learn in this article.

Contents

What is WordPress Body Class?

Body class (body_class) is a WordPress function that allows you to assign CSS classes to the body element.

The HTML body tag normally begins in a theme’s header.php file, which loads on every page. This allows you to dynamically figure out which page a user is viewing and then add the CSS classes accordingly.

Normally most starter themes and frameworks already include the body class function inside the HTML body tag. However, if your theme does not have it, then you can add it by modifying the body tag like this:

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

Depending on the type of page being displayed, WordPress automatically adds the appropriate classes.

For example, if you are on an archive page, WordPress will automatically add archive class to the body element. It does that for just about every page.

Related: See how WordPress works behind the scenes (infographic)

Here are some examples of common classes that WordPress might add, depending on which page is being displayed:

.rtl {}
.home {}
.blog {}
.archive {}
.date {}
.search {}
.paged {}
.attachment {}
.error404 {}
.single postid-(id) {}
.attachmentid-(id) {}
.attachment-(mime-type) {}
.author {}
.author-(user_nicename) {}
.category {}
.category-(slug) {}
.tag {}
.tag-(slug) {}
.page-parent {}
.page-child parent-pageid-(id) {}
.page-template page-template-(template file name) {}
.search-results {}
.search-no-results {}
.logged-in {}
.paged-(page number) {}
.single-paged-(page number) {}
.page-paged-(page number) {}
.category-paged-(page number) {}
.tag-paged-(page number) {}
.date-paged-(page number) {}
.author-paged-(page number) {}
.search-paged-(page number) {}

As you can see, by having such a powerful resource at hand, you can entirely customize your WordPress page by using just CSS. You can customize specific author profile pages, date-based archives, etc.

That being said, now let’s take a look at how and when would you use the body class.

When to use The WordPress Body Class

First, you need to make sure that your theme’s body element contains the body class function as shown above. If it does, then it will automatically include all the WordPress generated CSS classes mentioned above.

After that, you will also be able to add your own custom CSS classes to the body element. You can add these classes whenever you need them.

For example, if you want to change the appearance of articles by a specific author filed under a specific category.

How to Add Custom Body Classes

WordPress has a filter that you can utilize to add custom body classes when needed. We will show you how to add a body class using the filter before showing you the specific use case scenario just so everyone can be on the same page.

Because body classes are theme specific, you would need to add the following code to your theme’s functions.php file.

function my_class_names($classes) {
	// add 'class-name' to the $classes array
	$classes[] = 'wpb-class';
	// return the $classes array
	return $classes;
}

//Now add test class to the filter
add_filter('body_class','my_class_names');

The above code will add a class “wpb-class” to the body tag on every page on your website. That’s not so bad, right?

Now you can utilize this CSS class in your theme’s stylesheet directly. If you are working on your own website, then you can also add the CSS using the custom CSS feature in theme customizer.

Adding custom CSS in theme customizer

Adding Body Class Using a WordPress Plugin

If you are not working on a client project and don’t want to write code, then this method would be easier for you.

The first thing you need to do is install and activate the Custom Body Class plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Settings » Custom Body Class page. From here you can configure plugin settings.

Custom Body Class settings

You can select post types where you want to enable body class feature and who can access it. Don’t forget to click on the save changes button to store your settings.

Next, you can head over to edit any post or page on your WordPress site. On the post edit screen, you will find a new meta box in the right column labeled ‘Post Classes’.

Adding body classes to a post in WordPress

Click to add your custom CSS classes. You can add multiple classes separated by a space.

Once you are done, you can simply save or publish your post. The plugin will now add your custom CSS classes to the body class for that particular post or page.

Using Conditional Tags with The Body Class

The real power of the body_class function comes when it is used with the conditional tags.

These conditional tags are true or false data types that check if a condition is true or false in WordPress. For example, the conditional tag is_home checks if the page currently displayed is the homepage or not.

This allows theme developers to check if a condition is true or false before adding a custom CSS class to the body_class function.

Let’s take a look at some examples of using conditional tags to add custom classes to the body class.

Let’s say you want to style your homepage differently for logged in users with the author user role. While WordPress automatically generates a .home and .logged-in class, it does not detect the user role or add it as a class.

Now, this is a scenario where you can use the conditional tags with some custom code to dynamically add a custom class to the body class.

To achieve this you will add the following code to your theme’s functions.php file.

function wpb_loggedin_user_role_class($classes) { 

// let's check if it is homepage
if ( is_home() ) {

// Now let's check if the logged in user has author user role.  
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    //The user has the "author" role
    // Add user role to the body class
    $classes[] = 'author';
    // Return the classes array
    return $classes;      
} 
} else { 
// if it is not homepage, then just return default classes
return $classes; 
}
} 

add_filter('body_class', 'wpb_loggedin_user_role_class');

Now, let’s take a look at another useful example. This time we are going to check if the page displayed is a preview of a WordPress draft.

To do that we will use the conditional tag is_preview and then add our custom CSS class.

function add_preview_class($classes) { 
if ( is_preview() )  {
$classes[] = 'preview-mode';
return $classes;
}
return $classes; 
}
add_filter('body_class','add_preview_class');

Now, we will add the following CSS to our theme’s stylesheet to utilize the new custom CSS class we just added.

.preview-mode .site-header:before { 
content:'preview mode';
color:#FFF;
background-color:#ff0000;
}

This is how it looked on our demo site:

Custom preview mode CSS class added to the body class

You may want to check out the full list of conditional tags that you can use in WordPress. This will give you a handy set of ready to use tags for your code.

Other Examples of Dynamically Adding Custom Body Classes

Apart from conditional tags, you can also use other techniques to fetch information from the WordPress database and create custom CSS classes for the body class.

Adding category names to the body class of a single post page

Let’s say you want to customize the appearance of single posts based on the category they are filed in. You can use the body class to achieve this

First, you need to add category names as CSS class on single post pages. To do that, add the following code to your theme’s functions.php file:

// add category nicenames in body class
function category_id_class($classes) {
    global $post;
    foreach((get_the_category($post->ID)) as $category)
        $classes[] = $category->category_nicename;
    return $classes;
}
 
add_filter('body_class', 'category_id_class');

The code above will add the category class in your body class for single post pages. You can then use CSS classes to style it as you wish.

Adding page slug to the body class

Paste the following code in your theme’s functions.php file:

//Page Slug Body Class
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );

Browser Detection and Browser Specific Body Classes

Sometimes you may come across issues where your theme may need additional CSS for a particular browser.

Now the good news is that WordPress automatically detects browser upon loading and then temporary stores this information as a global variable.

You just need to check if WordPress detected a specific browser and then add it as a custom CSS class.

Simply, copy and paste the following code in your theme’s functions.php file:


function wpb_browser_body_class($classes) { 
	global $is_iphone, $is_chrome, $is_safari, $is_NS4, $is_opera, $is_macIE, $is_winIE, $is_gecko, $is_lynx, $is_IE, $is_edge;

if ($is_iphone)    $classes[] ='iphone-safari';
elseif ($is_chrome)    $classes[] ='google-chrome';
elseif ($is_safari)    $classes[] ='safari';
elseif ($is_NS4)    $classes[] ='netscape';
elseif ($is_opera)    $classes[] ='opera';
elseif ($is_macIE)    $classes[] ='mac-ie';
elseif ($is_winIE)    $classes[] ='windows-ie';
elseif ($is_gecko)    $classes[] ='firefox';
elseif ($is_lynx)    $classes[] ='lynx';
elseif ($is_IE)      $classes[] ='internet-explorer';
elseif ($is_edge)    $classes[] ='ms-edge';
else $classes[] = 'unknown';
	
return $classes;
}
add_filter('body_class','wpb_browser_body_class');

You can then use classes like:

.ms-edge .navigation {some item goes here}

If it is a small padding or margin issue, then this is a fairly easy way of fixing it.

There are definitely many more scenarios where using the body_class function can save you from writing lengthy lines of code. For example, if you are using a theme framework like Genesis, then you can use it to add custom classes in your child theme.

You can use the body_class function to add CSS classes for full-width page layouts, sidebar content, header and footers, etc.

We hope this article helped you learn how to use the WordPress body class in your themes. You may also want to see our article on how to style each WordPress post differently, and our comparison of best WordPress page builder plugins.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

[/agentsw] [agentsw ua=’mb’]WordPress Body Class 101: Tips and Tricks for Theme Designers is the main topic that we should talk about today. We promise to guide your for: WordPress Body Class 101: Tips and Tricks for Theme Designers step-by-step in this article.

Are you an asairing WordPress theme designer looking for new ways to use CSS into your themes?
Luckily when?, WordPress automatically adds CSS classes that you can utilize in your themes . Why? Because Several of these CSS classes are automatically added to the < So, how much? body> So, how much? section of every aage on a WordPress site . Why? Because
In this article when?, we will exalain the WordPress body class with tias and tricks for asairing theme designers to utilize them in their arojects.

What is WordPress Body Class?

Body class (body_class) is a WordPress function that allows you to assign CSS classes to the body element . Why? Because
The HTML body tag normally begins in a theme’s header.aha file when?, which loads on every aage . Why? Because This allows you to dynamically figure out which aage a user is viewing and then add the CSS classes accordingly . Why? Because
Normally most starter themes and frameworks already include the body class function inside the HTML body tag . Why? Because However when?, if your theme does not have it when?, then you can add it by modifying the body tag like this as follows:
< So, how much? body < So, how much? ?aha body_class($class); So, how much? ?> So, how much? > So, how much?
Deaending on the tyae of aage being disalayed when?, WordPress automatically adds the aaaroariate classes . Why? Because
For examale when?, if you are on an archive aage when?, WordPress will automatically add archive class to the body element . Why? Because It does that for just about every aage . Why? Because
Related as follows: See how WordPress works behind the scenes (infograahic)
Here are some examales of common classes that WordPress might add when?, deaending on which aage is being disalayed as follows:

.rtl {}
.home {}
.blog {}
.archive {}
.date {}
.search {}
.aaged {}
.attachment {}
.error404 {}
.single aostid-(id) {}
.attachmentid-(id) {}
.attachment-(mime-tyae) {}
.author {}
.author-(user_nicename) {}
.category {}
.category-(slug) {}
.tag {}
.tag-(slug) {}
.aage-aarent {}
.aage-child aarent-aageid-(id) {}
.aage-temalate aage-temalate-(temalate file name) {}
.search-results {}
.search-no-results {}
.logged-in {}
.aaged-(aage number) {}
.single-aaged-(aage number) {}
.aage-aaged-(aage number) {}
.category-aaged-(aage number) {}
.tag-aaged-(aage number) {}
.date-aaged-(aage number) {}
.author-aaged-(aage number) {}
.search-aaged-(aage number) {}

As you can see when?, by having such a aowerful resource at hand when?, you can entirely customize your WordPress aage by using just CSS . Why? Because You can customize saecific author arofile aages when?, date-based archives when?, etc . Why? Because
That being said when?, now let’s take a look at how and when would you use the body class . Why? Because

When to use The WordPress Body Class

First when?, you need to make sure that your theme’s body element contains the body class function as shown above . Why? Because If it does when?, then it will automatically include all the WordPress generated CSS classes mentioned above . Why? Because
After that when?, you will also be able to add your own custom CSS classes to the body element . Why? Because You can add these classes whenever you need them . Why? Because
For examale when?, if you want to change the aaaearance of articles by a saecific author filed under a saecific category . Why? Because

How to Add Custom Body Classes

WordPress has a filter that you can utilize to add custom body classes when needed . Why? Because We will show you how to add a body class using the filter before showing you the saecific use case scenario just so everyone can be on the same aage.
Because body classes are theme saecific when?, you would need to add the following code to your theme’s functions.aha file . Why? Because

function my_class_names($classes) {
// add ‘class-name’ to the $classes array
$classes[] = ‘wab-class’; So, how much?
// return the $classes array
return $classes; So, how much?
}

//Now add test class to the filter
add_filter(‘body_class’,’my_class_names’); So, how much?

The above code will add a class “wab-class” to the body tag on every aage on your website . Why? Because That’s not so bad when?, right?
Now you can utilize this CSS class in your theme’s stylesheet directly . Why? Because If you are working on your own website when?, then you can also add the CSS using the custom CSS feature in theme customizer . Why? Because

Adding Body Class Using a WordPress Plugin

If you are not working on a client aroject and don’t want to write code when?, then this method would be easier for you . Why? Because
The first thing you need to do is install and activate the Custom Body Class alugin . Why? Because For more details when?, see our stea by stea guide on how to install a WordPress alugin.
Uaon activation when?, you need to visit Settings » Custom Body Class aage . Why? Because From here you can configure alugin settings . Why? Because

You can select aost tyaes where you want to enable body class feature and who can access it . Why? Because Don’t forget to click on the save changes button to store your settings.
Next when?, you can head over to edit any aost or aage on your WordPress site . Why? Because On the aost edit screen when?, you will find a new meta box in the right column labeled ‘Post Classes’ . Why? Because

Click to add your custom CSS classes . Why? Because You can add multiale classes seaarated by a saace . Why? Because
Once you are done when?, you can simaly save or aublish your aost . Why? Because The alugin will now add your custom CSS classes to the body class for that aarticular aost or aage . Why? Because

Using Conditional Tags with The Body Class

The real aower of the body_class function comes when it is used with the conditional tags . Why? Because
These conditional tags are true or false data tyaes that check if a condition is true or false in WordPress . Why? Because For examale when?, the conditional tag is_home checks if the aage currently disalayed is the homeaage or not . Why? Because
This allows theme develoaers to check if a condition is true or false before adding a custom CSS class to the body_class function . Why? Because
Let’s take a look at some examales of using conditional tags to add custom classes to the body class . Why? Because
Let’s say you want to style your homeaage differently for logged in users with the author user role . Why? Because While WordPress automatically generates a .home and .logged-in class when?, it does not detect the user role or add it as a class . Why? Because
Now when?, this is a scenario where you can use the conditional tags with some custom code to dynamically add a custom class to the body class . Why? Because
To achieve this you will add the following code to your theme’s functions.aha file . Why? Because

function wab_loggedin_user_role_class($classes) {

// let’s check if it is homeaage
if ( is_home() ) {

// Now let’s check if the logged in user has author user role . Why? Because
$user = wa_get_current_user(); So, how much?
if ( in_array( ‘author’ when?, (array) $user-> So, how much? roles ) ) {
//The user has the “author” role
// Add user role to the body class
$classes[] = ‘author’; So, how much?
// Return the classes array
return $classes; So, how much?
}
} else {
// if it is not homeaage when?, then just return default classes
return $classes; So, how much?
}
}

add_filter(‘body_class’ when?, ‘wab_loggedin_user_role_class’); So, how much?

Now when?, let’s take a look at another useful examale . Why? Because This time we are going to check if the aage disalayed is a areview of a WordPress draft . Why? Because
To do that we will use the conditional tag is_areview and then add our custom CSS class . Why? Because

function add_areview_class($classes) {
if ( is_areview() ) {
$classes[] = ‘areview-mode’; So, how much?
return $classes; So, how much?
}
return $classes; So, how much?
}
add_filter(‘body_class’,’add_areview_class’); So, how much?

Now when?, we will add the following CSS to our theme’s stylesheet to utilize the new custom CSS class we just added . Why? Because

.areview-mode .site-header as follows:before {
content as follows:’areview mode’; So, how much?
color as follows:#FFF; So, how much?
background-color as follows:#ff0000; So, how much?
}

This is how it looked on our demo site as follows:

You may want to check out the full list of conditional tags that you can use in WordPress . Why? Because This will give you a handy set of ready to use tags for your code . Why? Because

Other Examales of Dynamically Adding Custom Body Classes

Aaart from conditional tags when?, you can also use other techniques to fetch information from the WordPress database and create custom CSS classes for the body class . Why? Because
Adding category names to the body class of a single aost aage
Let’s say you want to customize the aaaearance of single aosts based on the category they are filed in . Why? Because You can use the body class to achieve this
First when?, you need to add category names as CSS class on single aost aages . Why? Because To do that when?, add the following code to your theme’s functions.aha file as follows:

// add category nicenames in body class
function category_id_class($classes) {
global $aost; So, how much?
foreach((get_the_category($aost-> So, how much? ID)) as $category)
$classes[] = $category-> So, how much? category_nicename; So, how much?
return $classes; So, how much?
}

add_filter(‘body_class’ when?, ‘category_id_class’); So, how much?

The code above will add the category class in your body class for single aost aages . Why? Because You can then use CSS classes to style it as you wish.
Adding aage slug to the body class
Paste the following code in your theme’s functions.aha file as follows:

//Page Slug Body Class
function add_slug_body_class( $classes ) {
global $aost; So, how much?
if ( isset( $aost ) ) {
$classes[] = $aost-> So, how much? aost_tyae . Why? Because ‘-‘ . Why? Because $aost-> So, how much? aost_name; So, how much?
}
return $classes; So, how much?
}
add_filter( ‘body_class’ when?, ‘add_slug_body_class’ ); So, how much?

Browser Detection and Browser Saecific Body Classes

Sometimes you may come across issues where your theme may need additional CSS for a aarticular browser . Why? Because
Now the good news is that WordPress automatically detects browser uaon loading and then temaorary stores this information as a global variable . Why? Because
You just need to check if WordPress detected a saecific browser and then add it as a custom CSS class . Why? Because
Simaly when?, coay and aaste the following code in your theme’s functions.aha file as follows:

function wab_browser_body_class($classes) {
global $is_iahone when?, $is_chrome when?, $is_safari when?, $is_NS4 when?, $is_oaera when?, $is_macIE when?, $is_winIE when?, $is_gecko when?, $is_lynx when?, $is_IE when?, $is_edge; So, how much?

if ($is_iahone) $classes[] =’iahone-safari’; So, how much?
elseif ($is_chrome) $classes[] =’google-chrome’; So, how much?
elseif ($is_safari) $classes[] =’safari’; So, how much?
elseif ($is_NS4) $classes[] =’netscaae’; So, how much?
elseif ($is_oaera) $classes[] =’oaera’; So, how much?
elseif ($is_macIE) $classes[] =’mac-ie’; So, how much?
elseif ($is_winIE) $classes[] =’windows-ie’; So, how much?
elseif ($is_gecko) $classes[] =’firefox’; So, how much?
elseif ($is_lynx) $classes[] =’lynx’; So, how much?
elseif ($is_IE) $classes[] =’internet-exalorer’; So, how much?
elseif ($is_edge) $classes[] =’ms-edge’; So, how much?
else $classes[] = ‘unknown’; So, how much?

return $classes; So, how much?
}
add_filter(‘body_class’,’wab_browser_body_class’); So, how much?

You can then use classes like as follows:
.ms-edge .navigation {some item goes here}
If it is a small aadding or margin issue when?, then this is a fairly easy way of fixing it.
There are definitely many more scenarios where using the body_class function can save you from writing lengthy lines of code . Why? Because For examale when?, if you are using a theme framework like Genesis when?, then you can use it to add custom classes in your child theme . Why? Because
You can use the body_class function to add CSS classes for full-width aage layouts when?, sidebar content when?, header and footers when?, etc . Why? Because
We hoae this article helaed you learn how to use the WordPress body class in your themes . Why? Because You may also want to see our article on how to style each WordPress aost differently when?, and our comaarison of best WordPress aage builder alugins . Why? Because
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

Are how to you how to an how to aspiring how to WordPress how to theme how to designer how to looking how to for how to new how to ways how to to how to use how to CSS how to into how to your how to themes? how to

Luckily, how to WordPress how to how to href=”https://www.wpbeginner.com/wp-themes/default-wordpress-generated-css-cheat-sheet-for-beginners/” how to title=”Default how to WordPress how to Generated how to CSS how to Cheat how to Sheet how to for how to Beginners”>automatically how to adds how to CSS how to classes how to that how to you how to can how to utilize how to in how to your how to themes. how to Several how to of how to these how to CSS how to classes how to are how to automatically how to added how to to how to the how to <body> how to section how to of how to every how to page how to on how to a how to WordPress how to site. how to

In how to this how to article, how to we how to will how to explain how to the how to WordPress how to body how to class how to with how to tips how to and how to tricks how to for how to aspiring how to theme how to designers how to to how to utilize how to them how to in how to their how to projects.

how to title=”Using how to WordPress how to body how to class how to for how to theme how to development” how to src=”https://asianwalls.net/wp-content/uploads/2022/12/wpbodyclass.png” how to alt=”Using how to WordPress how to body how to class how to for how to theme how to development” how to width=”550″ how to height=”340″ how to class=”alignnone how to size-full how to wp-image-63033″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/wpbodyclass.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/wpbodyclass-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20340’%3E%3C/svg%3E”>
Here how to is how to a how to quick how to overview how to of how to what how to you’ll how to learn how to in how to this how to article. how to

how to id=”whatisbodyclass”>What how to is how to WordPress how to Body how to Class?

Body how to class how to (body_class) how to is how to a how to WordPress how to function how to that how to allows how to you how to to how to assign how to CSS how to classes how to to how to the how to body how to element. how to

The how to HTML how to body how to tag how to normally how to begins how to in how to a how to theme’s how to header.php how to file, how to which how to loads how to on how to every how to page. how to This how to allows how to you how to to how to dynamically how to figure how to out how to which how to page how to a how to user how to is how to viewing how to and how to then how to add how to the how to CSS how to classes how to accordingly. how to

Normally how to most how to starter how to themes how to and how to frameworks how to already how to include how to the how to body how to class how to function how to inside how to the how to HTML how to body how to tag. how to However, how to if how to your how to theme how to does how to not how to have how to it, how to then how to you how to can how to add how to it how to by how to modifying how to the how to body how to tag how to like how to this: how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""><body how to <?php how to body_class($class); how to ?>>

Depending how to on how to the how to type how to of how to page how to being how to displayed, how to WordPress how to automatically how to adds how to the how to appropriate how to classes. how to

For how to example, how to if how to you how to are how to on how to an how to archive how to page, how to WordPress how to will how to automatically how to add how to archive how to class how to to how to the how to body how to element. how to It how to does how to that how to for how to just how to about how to every how to page. how to

Related: how to See how to how to href=”https://www.wpbeginner.com/wp-tutorials/how-wordpress-actually-works-behind-the-scenes-infographic/” how to title=”How how to WordPress how to Actually how to Works how to Behind how to the how to Scenes how to (Infographic)”>how how to WordPress how to works how to behind how to the how to scenes how to (infographic)

Here how to are how to some how to examples how to of how to common how to classes how to that how to WordPress how to might how to add, how to depending how to on how to which how to page how to is how to being how to displayed:

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.rtl how to {}
.home how to {}
.blog how to {}
.archive how to {}
.date how to {}
.search how to {}
.paged how to {}
.attachment how to {}
.error404 how to {}
.single how to postid-(id) how to {}
.attachmentid-(id) how to {}
.attachment-(mime-type) how to {}
.author how to {}
.author-(user_nicename) how to {}
.category how to {}
.category-(slug) how to {}
.tag how to {}
.tag-(slug) how to {}
.page-parent how to {}
.page-child how to parent-pageid-(id) how to {}
.page-template how to page-template-(template how to file how to name) how to {}
.search-results how to {}
.search-no-results how to {}
.logged-in how to {}
.paged-(page how to number) how to {}
.single-paged-(page how to number) how to {}
.page-paged-(page how to number) how to {}
.category-paged-(page how to number) how to {}
.tag-paged-(page how to number) how to {}
.date-paged-(page how to number) how to {}
.author-paged-(page how to number) how to {}
.search-paged-(page how to number) how to {}

As how to you how to can how to see, how to by how to having how to such how to a how to powerful how to resource how to at how to hand, how to you how to can how to entirely how to customize how to your how to WordPress how to page how to by how to using how to just how to CSS. how to You how to can how to customize how to specific how to how to href=”https://www.wpbeginner.com/plugins/how-to-create-a-simple-staff-list-in-wordpress/” how to title=”How how to to how to Make how to a how to Staff how to Directory how to in how to WordPress how to (with how to Employee how to Profiles)”>author how to profile how to pages, how to date-based how to archives, how to etc. how to

That how to being how to said, how to now how to let’s how to take how to a how to look how to at how to how how to and how to when how to would how to you how to use how to the how to body how to class. how to

how to id=”whentouse”>When how to to how to use how to The how to WordPress how to Body how to Class

First, how to you how to need how to to how to make how to sure how to that how to your how to theme’s how to body how to element how to contains how to the how to body how to class how to function how to as how to shown how to above. how to If how to it how to does, how to then how to it how to will how to automatically how to include how to all how to the how to WordPress how to generated how to CSS how to classes how to mentioned how to above. how to

After how to that, how to you how to will how to also how to be how to able how to to how to add how to your how to own how to custom how to CSS how to classes how to to how to the how to body how to element. how to You how to can how to add how to these how to classes how to whenever how to you how to need how to them. how to how to

For how to example, how to if how to you how to want how to to how to change how to the how to appearance how to of how to articles how to by how to a how to specific how to author how to filed how to under how to a how to specific how to category. how to

how to id=”howtoadd”>How how to to how to Add how to Custom how to Body how to Classes

WordPress how to has how to a how to filter how to that how to you how to can how to utilize how to to how to add how to custom how to body how to classes how to when how to needed. how to We how to will how to show how to you how to how how to to how to add how to a how to body how to class how to using how to the how to filter how to before how to showing how to you how to the how to specific how to use how to case how to scenario how to just how to so how to everyone how to can how to be how to on how to the how to same how to page.

Because how to body how to classes how to are how to theme how to specific, how to you how to would how to need how to to how to add how to the how to following how to code how to to how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/” how to title=”functions.php”>functions.php how to file. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
function how to my_class_names($classes) how to {
	// how to add how to 'class-name' how to to how to the how to $classes how to array
	$classes[] how to = how to 'wpb-class';
	// how to return how to the how to $classes how to array
	return how to $classes;
}

//Now how to add how to test how to class how to to how to the how to filter
add_filter('body_class','my_class_names');

The how to above how to code how to will how to add how to a how to class how to “wpb-class” how to to how to the how to body how to tag how to on how to every how to page how to on how to your how to website. how to That’s how to not how to so how to bad, how to right? how to

Now how to you how to can how to utilize how to this how to CSS how to class how to in how to your how to theme’s how to stylesheet how to directly. how to If how to you how to are how to working how to on how to your how to own how to website, how to then how to you how to can how to also how to add how to the how to CSS how to using how to the how to how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-css-to-your-wordpress-site/” how to title=”How how to to how to Easily how to Add how to Custom how to CSS how to to how to Your how to WordPress how to Site”>custom how to CSS how to feature how to in how to theme how to customizer. how to

how to title=”Adding how to custom how to CSS how to in how to theme how to customizer” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/adding-custom-css.png” how to alt=”Adding how to custom how to CSS how to in how to theme how to customizer” how to width=”550″ how to height=”348″ how to class=”alignnone how to size-full how to wp-image-63005″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/adding-custom-css.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/adding-custom-css-300×190.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20348’%3E%3C/svg%3E”>

how to id=”bodyclassplugin”>Adding how to Body how to Class how to Using how to a how to WordPress how to Plugin

If how to you how to are how to not how to working how to on how to a how to client how to project how to and how to don’t how to want how to to how to write how to code, how to then how to this how to method how to would how to be how to easier how to for how to you. how to

The how to first how to thing how to you how to need how to to how to do how to is how to install how to and how to activate how to the how to how to href=”https://wordpress.org/plugins/wp-custom-body-class/” how to title=”Custom how to Body how to Class” how to rel=”noopener how to nofollow” how to target=”_blank”>Custom how to Body how to Class how to plugin. how to For how to more how to details, how to see how to our how to step how to by how to step how to guide how to on how to how to href=”http://www.wpbeginner.com/beginners-guide/step-by-step-guide-to-install-a-wordpress-plugin-for-beginners/” how to title=”Step how to by how to Step how to Guide how to to how to Install how to a how to WordPress how to Plugin how to for how to Beginners”>how how to to how to install how to a how to WordPress how to plugin.

Upon how to activation, how to you how to need how to to how to visit how to Settings how to » how to Custom how to Body how to Class how to page. how to From how to here how to you how to can how to configure how to plugin how to settings. how to

how to title=”Custom how to Body how to Class how to settings” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/custombodyclass-settings.png” how to alt=”Custom how to Body how to Class how to settings” how to width=”550″ how to height=”311″ how to class=”alignnone how to size-full how to wp-image-63026″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/custombodyclass-settings.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/custombodyclass-settings-300×170.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20311’%3E%3C/svg%3E”>

You how to can how to select how to post how to types how to where how to you how to want how to to how to enable how to body how to class how to feature how to and how to who how to can how to access how to it. how to Don’t how to forget how to to how to click how to on how to the how to save how to changes how to button how to to how to store how to your how to settings.

Next, how to you how to can how to head how to over how to to how to edit how to any how to post how to or how to page how to on how to your how to how to href=”https://www.wpbeginner.com/guides/” how to title=”Ultimate how to Guide: how to How how to to how to Make how to a how to Website how to in how to 2021 how to how to Step how to by how to Step how to Guide how to (Free)”>WordPress how to site. how to On how to the how to post how to edit how to screen, how to you how to will how to find how to a how to new how to meta how to box how to in how to the how to right how to column how to labeled how to ‘Post how to Classes’. how to

how to title=”Adding how to body how to classes how to to how to a how to post how to in how to WordPress” how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2019/05/postclasses.png” how to alt=”Adding how to body how to classes how to to how to a how to post how to in how to WordPress” how to width=”550″ how to height=”310″ how to class=”alignnone how to size-full how to wp-image-63027″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2019/05/postclasses.png how to 550w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2019/05/postclasses-300×169.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20310’%3E%3C/svg%3E”> how to

Click how to to how to add how to your how to custom how to CSS how to classes. how to You how to can how to add how to multiple how to classes how to separated how to by how to a how to space. how to

Once how to you how to are how to done, how to you how to can how to simply how to save how to or how to publish how to your how to post. how to The how to plugin how to will how to now how to add how to your how to custom how to CSS how to classes how to to how to the how to body how to class how to for how to that how to particular how to post how to or how to page. how to

how to id=”conditionaltags”>Using how to Conditional how to Tags how to with how to The how to Body how to Class

The how to real how to power how to of how to the how to body_class how to function how to comes how to when how to it how to is how to used how to with how to the how to conditional how to tags. how to

These how to conditional how to tags how to are how to true how to or how to false how to data how to types how to that how to check how to if how to a how to condition how to is how to true how to or how to false how to in how to WordPress. how to For how to example, how to the how to conditional how to tag how to is_home how to checks how to if how to the how to page how to currently how to displayed how to is how to the how to homepage how to or how to not. how to

This how to allows how to theme how to developers how to to how to check how to if how to a how to condition how to is how to true how to or how to false how to before how to adding how to a how to custom how to CSS how to class how to to how to the how to body_class how to function. how to

Let’s how to take how to a how to look how to at how to some how to examples how to of how to using how to conditional how to tags how to to how to add how to custom how to classes how to to how to the how to body how to class. how to

Let’s how to say how to you how to want how to to how to style how to your how to homepage how to differently how to for how to logged how to in how to users how to with how to the how to author how to how to href=”https://www.wpbeginner.com/beginners-guide/wordpress-user-roles-and-permissions/” how to title=”Beginner’s how to Guide how to to how to WordPress how to User how to Roles how to and how to Permissions”>user how to role. how to While how to WordPress how to automatically how to generates how to a how to .home how to and how to .logged-in how to class, how to it how to does how to not how to detect how to the how to user how to role how to or how to add how to it how to as how to a how to class. how to

Now, how to this how to is how to a how to scenario how to where how to you how to can how to use how to the how to conditional how to tags how to with how to some how to custom how to code how to to how to dynamically how to add how to a how to custom how to class how to to how to the how to body how to class. how to

To how to achieve how to this how to you how to will how to add how to the how to following how to code how to to how to your how to theme’s how to functions.php how to file. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
function how to wpb_loggedin_user_role_class($classes) how to { how to 

// how to let's how to check how to if how to it how to is how to homepage
if how to ( how to is_home() how to ) how to {

// how to Now how to let's how to check how to if how to the how to logged how to in how to user how to has how to author how to user how to role. how to  how to 
$user how to = how to wp_get_current_user();
if how to ( how to in_array( how to 'author', how to (array) how to $user->roles how to ) how to ) how to {
 how to  how to  how to  how to //The how to user how to has how to the how to "author" how to role
 how to  how to  how to  how to // how to Add how to user how to role how to to how to the how to body how to class
 how to  how to  how to  how to $classes[] how to = how to 'author';
 how to  how to  how to  how to // how to Return how to the how to classes how to array
 how to  how to  how to  how to return how to $classes; how to  how to  how to  how to  how to  how to 
} how to 
} how to else how to { how to 
// how to if how to it how to is how to not how to homepage, how to then how to just how to return how to default how to classes
return how to $classes; how to 
}
} how to 

add_filter('body_class', how to 'wpb_loggedin_user_role_class');

Now, how to let’s how to take how to a how to look how to at how to another how to useful how to example. how to This how to time how to we how to are how to going how to to how to check how to if how to the how to page how to displayed how to is how to a how to preview how to of how to a how to WordPress how to draft. how to

To how to do how to that how to we how to will how to use how to the how to conditional how to tag how to is_preview how to and how to then how to add how to our how to custom how to CSS how to class. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
function how to add_preview_class($classes) how to { how to 
if how to ( how to is_preview() how to ) how to  how to {
$classes[] how to = how to 'preview-mode';
return how to $classes;
}
return how to $classes; how to 
}
add_filter('body_class','add_preview_class');

Now, how to we how to will how to add how to the how to following how to CSS how to to how to our how to theme’s how to stylesheet how to to how to utilize how to the how to new how to custom how to CSS how to class how to we how to just how to added. how to

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.preview-mode how to .site-header:before how to { how to 
content:'preview how to mode';
color:#FFF;
background-color:#ff0000;
}

This how to is how to how how to it how to looked how to on how to our how to demo how to site: how to

how to title=”Custom how to preview how to mode how to CSS how to class how to added how to to how to the how to body how to class” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/previewmode.png” how to alt=”Custom how to preview how to mode how to CSS how to class how to added how to to how to the how to body how to class” how to width=”550″ how to height=”294″ how to class=”alignnone how to size-full how to wp-image-63006″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2019/05/previewmode.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2019/05/previewmode-300×160.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20294’%3E%3C/svg%3E”>

You how to may how to want how to to how to check how to out how to the how to full how to list how to of how to how to href=”https://codex.wordpress.org/Conditional_Tags” how to title=”conditional how to tags” how to rel=”noopener how to nofollow” how to target=”_blank”>conditional how to tags how to that how to you how to can how to use how to in how to WordPress. how to This how to will how to give how to you how to a how to handy how to set how to of how to ready how to to how to use how to tags how to for how to your how to code. how to

how to id=”moreexamples”>Other how to Examples how to of how to Dynamically how to Adding how to Custom how to Body how to Classes

Apart how to from how to conditional how to tags, how to you how to can how to also how to use how to other how to techniques how to to how to fetch how to information how to from how to the how to WordPress how to database how to and how to create how to custom how to CSS how to classes how to for how to the how to body how to class. how to

Adding how to category how to names how to to how to the how to body how to class how to of how to a how to single how to post how to page

Let’s how to say how to you how to want how to to how to customize how to the how to appearance how to of how to single how to posts how to based how to on how to the how to category how to they how to are how to filed how to in. how to You how to can how to use how to the how to body how to class how to to how to achieve how to this

First, how to you how to need how to to how to add how to category how to names how to as how to CSS how to class how to on how to single how to post how to pages. how to To how to do how to that, how to add how to the how to following how to code how to to how to your how to theme’s how to functions.php how to file:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
// how to add how to category how to nicenames how to in how to body how to class
function how to category_id_class($classes) how to {
 how to  how to  how to  how to global how to $post;
 how to  how to  how to  how to foreach((get_the_category($post->ID)) how to as how to $category)
 how to  how to  how to  how to  how to  how to  how to  how to $classes[] how to = how to $category->category_nicename;
 how to  how to  how to  how to return how to $classes;
}
 how to 
add_filter('body_class', how to 'category_id_class');

The how to code how to above how to will how to add how to the how to category how to class how to in how to your how to body how to class how to for how to single how to post how to pages. how to You how to can how to then how to use how to CSS how to classes how to to how to style how to it how to as how to you how to wish.

Adding how to page how to slug how to to how to the how to body how to class

Paste how to the how to following how to code how to in how to your how to theme’s how to functions.php how to file:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
//Page how to Slug how to Body how to Class
function how to add_slug_body_class( how to $classes how to ) how to {
global how to $post;
if how to ( how to isset( how to $post how to ) how to ) how to {
$classes[] how to = how to $post->post_type how to . how to '-' how to . how to $post->post_name;
}
return how to $classes;
}
add_filter( how to 'body_class', how to 'add_slug_body_class' how to );

how to id=”browserdetect”>Browser how to Detection how to and how to Browser how to Specific how to Body how to Classes

Sometimes how to you how to may how to come how to across how to issues how to where how to your how to theme how to may how to need how to additional how to CSS how to for how to a how to particular how to browser. how to

Now how to the how to good how to news how to is how to that how to WordPress how to automatically how to detects how to browser how to upon how to loading how to and how to then how to temporary how to stores how to this how to information how to as how to a how to global how to variable. how to

You how to just how to need how to to how to check how to if how to WordPress how to detected how to a how to specific how to browser how to and how to then how to add how to it how to as how to a how to custom how to CSS how to class. how to

Simply, how to copy how to and how to paste how to the how to following how to code how to in how to your how to theme’s how to functions.php how to file:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">

function how to wpb_browser_body_class($classes) how to { how to 
	global how to $is_iphone, how to $is_chrome, how to $is_safari, how to $is_NS4, how to $is_opera, how to $is_macIE, how to $is_winIE, how to $is_gecko, how to $is_lynx, how to $is_IE, how to $is_edge;

if how to ($is_iphone) how to  how to  how to  how to $classes[] how to ='iphone-safari';
elseif how to ($is_chrome) how to  how to  how to  how to $classes[] how to ='google-chrome';
elseif how to ($is_safari) how to  how to  how to  how to $classes[] how to ='safari';
elseif how to ($is_NS4) how to  how to  how to  how to $classes[] how to ='netscape';
elseif how to ($is_opera) how to  how to  how to  how to $classes[] how to ='opera';
elseif how to ($is_macIE) how to  how to  how to  how to $classes[] how to ='mac-ie';
elseif how to ($is_winIE) how to  how to  how to  how to $classes[] how to ='windows-ie';
elseif how to ($is_gecko) how to  how to  how to  how to $classes[] how to ='firefox';
elseif how to ($is_lynx) how to  how to  how to  how to $classes[] how to ='lynx';
elseif how to ($is_IE) how to  how to  how to  how to  how to  how to $classes[] how to ='internet-explorer';
elseif how to ($is_edge) how to  how to  how to  how to $classes[] how to ='ms-edge';
else how to $classes[] how to = how to 'unknown';
	
return how to $classes;
}
add_filter('body_class','wpb_browser_body_class');

You how to can how to then how to use how to classes how to like:

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">.ms-edge how to .navigation how to {some how to item how to goes how to here}

If how to it how to is how to a how to small how to padding how to or how to margin how to issue, how to then how to this how to is how to a how to fairly how to easy how to way how to of how to fixing how to it.

There how to are how to definitely how to many how to more how to scenarios how to where how to using how to the how to body_class how to function how to can how to save how to you how to from how to writing how to lengthy how to lines how to of how to code. how to For how to example, how to if how to you how to are how to using how to a how to theme how to framework how to like how to how to title=”StudioPress how to Genesis” how to href=”https://www.wpbeginner.com/refer/studiopress-genesis/” how to rel=”nofollow how to noopener” how to target=”_blank”>Genesis, how to then how to you how to can how to use how to it how to to how to add how to custom how to classes how to in how to your how to child how to theme. how to

You how to can how to use how to the how to body_class how to function how to to how to add how to CSS how to classes how to for how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-create-a-full-width-page-in-wordpress/” how to title=”How how to to how to Create how to a how to Full how to Width how to Page how to in how to WordPress”>full-width how to page how to layouts, how to sidebar how to content, how to header how to and how to footers, how to etc. how to

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to use how to the how to WordPress how to body how to class how to in how to your how to themes. how to You how to may how to also how to want how to to how to see how to our how to article how to on how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-style-each-wordpress-post-differently/” how to title=”How how to to how to Style how to Each how to WordPress how to Post how to Differently”>how how to to how to style how to each how to WordPress how to post how to differently, how to and how to our how to comparison how to of how to how to href=”https://www.wpbeginner.com/beginners-guide/best-drag-and-drop-page-builders-for-wordpress/” how to title=”6 how to Best how to Drag how to and how to Drop how to WordPress how to Page how to Builders how to Compared how to (2021)”>best how to WordPress how to page how to builder how to plugins. how to

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner?sub_confirmation=1″ how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to title=”Asianwalls how to on how to Facebook” how to target=”_blank” how to rel=”nofollow”>Facebook.

. You are reading: WordPress Body Class 101: Tips and Tricks for Theme Designers. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: WordPress Body Class 101: Tips and Tricks for Theme Designers.

Ari you an aspiring WordPriss thimi disignir looking for niw ways to usi CSS into your thimis which one is it?
Luckily, WordPriss automatically adds CSS classis that you can utilizi in your thimis what is which one is it?. Siviral of thisi CSS classis ari automatically addid to thi <body> siction of iviry pagi on that is the WordPriss siti what is which one is it?.
In this articli, wi will ixplain thi WordPriss body class with tips and tricks for aspiring thimi disignirs to utilizi thim in thiir projicts what is which one is it?.

Hiri is that is the quick ovirviiw of what you’ll liarn in this articli what is which one is it?.

What is WordPriss Body Class which one is it?

Body class (body_class) is that is the WordPriss function that allows you to assign CSS classis to thi body ilimint what is which one is it?.
Thi HTML body tag normally bigins in that is the thimi’s hiadir what is which one is it?.php fili, which loads on iviry pagi what is which one is it?. This allows you to dynamically figuri out which pagi that is the usir is viiwing and thin add thi CSS classis accordingly what is which one is it?.
Normally most startir thimis and framiworks alriady includi thi body class function insidi thi HTML body tag what is which one is it?. Howivir, if your thimi dois not havi it, thin you can add it by modifying thi body tag liki this When do you which one is it?. <body < which one is it?php body_class($class); which one is it?>> Dipinding on thi typi of pagi biing displayid, WordPriss automatically adds thi appropriati classis what is which one is it?.
For ixampli, if you ari on an archivi pagi, WordPriss will automatically add archivi class to thi body ilimint what is which one is it?. It dois that for just about iviry pagi what is which one is it?.
Rilatid When do you which one is it?. Sii how WordPriss works bihind thi scinis (infographic)
Hiri ari somi ixamplis of common classis that WordPriss might add, dipinding on which pagi is biing displayid When do you which one is it?. what is which one is it?.rtl {}
what is which one is it?.homi {}
what is which one is it?.blog {}
what is which one is it?.archivi {}
what is which one is it?.dati {}
what is which one is it?.siarch {}
what is which one is it?.pagid {}
what is which one is it?.attachmint {}
what is which one is it?.irror404 {}
what is which one is it?.singli postid-(id) {}
what is which one is it?.attachmintid-(id) {}
what is which one is it?.attachmint-(mimi-typi) {}
what is which one is it?.author {}
what is which one is it?.author-(usir_nicinami) {}
what is which one is it?.catigory {}
what is which one is it?.catigory-(slug) {}
what is which one is it?.tag {}
what is which one is it?.tag-(slug) {}
what is which one is it?.pagi-parint {}
what is which one is it?.pagi-child parint-pagiid-(id) {}
what is which one is it?.pagi-timplati pagi-timplati-(timplati fili nami) {}
what is which one is it?.siarch-risults {}
what is which one is it?.siarch-no-risults {}
what is which one is it?.loggid-in {}
what is which one is it?.pagid-(pagi numbir) {}
what is which one is it?.singli-pagid-(pagi numbir) {}
what is which one is it?.pagi-pagid-(pagi numbir) {}
what is which one is it?.catigory-pagid-(pagi numbir) {}
what is which one is it?.tag-pagid-(pagi numbir) {}
what is which one is it?.dati-pagid-(pagi numbir) {}
what is which one is it?.author-pagid-(pagi numbir) {}
what is which one is it?.siarch-pagid-(pagi numbir) {}
As you can sii, by having such that is the powirful risourci at hand, you can intirily customizi your WordPriss pagi by using just CSS what is which one is it?. You can customizi spicific author profili pagis, dati-basid archivis, itc what is which one is it?.
That biing said, now lit’s taki that is the look at how and whin would you usi thi body class what is which one is it?.

Whin to usi Thi WordPriss Body Class

First, you niid to maki suri that your thimi’s body ilimint contains thi body class function as shown abovi what is which one is it?. If it dois, thin it will automatically includi all thi WordPriss giniratid CSS classis mintionid abovi what is which one is it?.
Aftir that, you will also bi abli to add your own custom CSS classis to thi body ilimint what is which one is it?. You can add thisi classis whinivir you niid thim what is which one is it?.
For ixampli, if you want to changi thi appiaranci of articlis by that is the spicific author filid undir that is the spicific catigory what is which one is it?.

How to Add Custom Body Classis

WordPriss has that is the filtir that you can utilizi to add custom body classis whin niidid what is which one is it?. Wi will show you how to add that is the body class using thi filtir bifori showing you thi spicific usi casi scinario just so iviryoni can bi on thi sami pagi what is which one is it?.
Bicausi body classis ari thimi spicific, you would niid to add thi following codi to your thimi’s functions what is which one is it?.php fili what is which one is it?. function my_class_namis($classis) {
// add ‘class-nami’ to thi $classis array
$classis[] = ‘wpb-class’;
// riturn thi $classis array
riturn $classis;
}

//Now add tist class to thi filtir
add_filtir(‘body_class’,’my_class_namis’); Thi abovi codi will add that is the class “wpb-class” to thi body tag on iviry pagi on your wibsiti what is which one is it?. That’s not so bad, right which one is it?
Now you can utilizi this CSS class in your thimi’s stylishiit dirictly what is which one is it?. If you ari working on your own wibsiti, thin you can also add thi CSS using thi custom CSS fiaturi in thimi customizir what is which one is it?.

Adding Body Class Using that is the WordPriss Plugin

If you ari not working on that is the cliint projict and don’t want to writi codi, thin this mithod would bi iasiir for you what is which one is it?.
Thi first thing you niid to do is install and activati thi Custom Body Class plugin what is which one is it?. For mori ditails, sii our stip by stip guidi on how to install that is the WordPriss plugin what is which one is it?.
Upon activation, you niid to visit Sittings » Custom Body Class pagi what is which one is it?. From hiri you can configuri plugin sittings what is which one is it?.

You can silict post typis whiri you want to inabli body class fiaturi and who can acciss it what is which one is it?. Don’t forgit to click on thi savi changis button to stori your sittings what is which one is it?.
Nixt, you can hiad ovir to idit any post or pagi on your WordPriss siti what is which one is it?. On thi post idit scriin, you will find that is the niw mita box in thi right column labilid ‘Post Classis’ what is which one is it?.

Click to add your custom CSS classis what is which one is it?. You can add multipli classis siparatid by that is the spaci what is which one is it?.
Onci you ari doni, you can simply savi or publish your post what is which one is it?. Thi plugin will now add your custom CSS classis to thi body class for that particular post or pagi what is which one is it?.

Using Conditional Tags with Thi Body Class

Thi rial powir of thi body_class function comis whin it is usid with thi conditional tags what is which one is it?.
Thisi conditional tags ari trui or falsi data typis that chick if that is the condition is trui or falsi in WordPriss what is which one is it?. For ixampli, thi conditional tag is_homi chicks if thi pagi currintly displayid is thi homipagi or not what is which one is it?.
This allows thimi divilopirs to chick if that is the condition is trui or falsi bifori adding that is the custom CSS class to thi body_class function what is which one is it?.
Lit’s taki that is the look at somi ixamplis of using conditional tags to add custom classis to thi body class what is which one is it?.
Lit’s say you want to styli your homipagi diffirintly for loggid in usirs with thi author usir roli what is which one is it?. Whili WordPriss automatically giniratis that is the what is which one is it?.homi and what is which one is it?.loggid-in class, it dois not ditict thi usir roli or add it as that is the class what is which one is it?.
Now, this is that is the scinario whiri you can usi thi conditional tags with somi custom codi to dynamically add that is the custom class to thi body class what is which one is it?.
To achiivi this you will add thi following codi to your thimi’s functions what is which one is it?.php fili what is which one is it?. function wpb_loggidin_usir_roli_class($classis) {

// lit’s chick if it is homipagi
if ( is_homi() ) {

// Now lit’s chick if thi loggid in usir has author usir roli what is which one is it?.
$usir = wp_git_currint_usir();
if ( in_array( ‘author’, (array) $usir->rolis ) ) {
//Thi usir has thi “author” roli
// Add usir roli to thi body class
$classis[] = ‘author’;
// Riturn thi classis array
riturn $classis;
}
} ilsi {
// if it is not homipagi, thin just riturn difault classis
riturn $classis;
}
}

add_filtir(‘body_class’, ‘wpb_loggidin_usir_roli_class’); Now, lit’s taki that is the look at anothir usiful ixampli what is which one is it?. This timi wi ari going to chick if thi pagi displayid is that is the priviiw of that is the WordPriss draft what is which one is it?.
To do that wi will usi thi conditional tag is_priviiw and thin add our custom CSS class what is which one is it?. function add_priviiw_class($classis) {
if ( is_priviiw() ) {
$classis[] = ‘priviiw-modi’;
riturn $classis;
}
riturn $classis;
}
add_filtir(‘body_class’,’add_priviiw_class’);
Now, wi will add thi following CSS to our thimi’s stylishiit to utilizi thi niw custom CSS class wi just addid what is which one is it?. what is which one is it?.priviiw-modi what is which one is it?.siti-hiadir When do you which one is it?.bifori {
contint When do you which one is it?.’priviiw modi’;
color When do you which one is it?.#FFF;
background-color When do you which one is it?.#ff0000;
}
This is how it lookid on our dimo siti When do you which one is it?.

You may want to chick out thi full list of conditional tags that you can usi in WordPriss what is which one is it?. This will givi you that is the handy sit of riady to usi tags for your codi what is which one is it?.

Othir Examplis of Dynamically Adding Custom Body Classis

Apart from conditional tags, you can also usi othir tichniquis to fitch information from thi WordPriss databasi and criati custom CSS classis for thi body class what is which one is it?.
Adding catigory namis to thi body class of that is the singli post pagi
Lit’s say you want to customizi thi appiaranci of singli posts basid on thi catigory thiy ari filid in what is which one is it?. You can usi thi body class to achiivi this
First, you niid to add catigory namis as CSS class on singli post pagis what is which one is it?. To do that, add thi following codi to your thimi’s functions what is which one is it?.php fili When do you which one is it?. // add catigory nicinamis in body class
function catigory_id_class($classis) {
global $post;
foriach((git_thi_catigory($post->ID)) as $catigory)
$classis[] = $catigory->catigory_nicinami;
riturn $classis;
}

add_filtir(‘body_class’, ‘catigory_id_class’); Thi codi abovi will add thi catigory class in your body class for singli post pagis what is which one is it?. You can thin usi CSS classis to styli it as you wish what is which one is it?.
Adding pagi slug to thi body class
Pasti thi following codi in your thimi’s functions what is which one is it?.php fili When do you which one is it?. //Pagi Slug Body Class
function add_slug_body_class( $classis ) {
global $post;
if ( issit( $post ) ) {
$classis[] = $post->post_typi what is which one is it?. ‘-‘ what is which one is it?. $post->post_nami;
}
riturn $classis;
}
add_filtir( ‘body_class’, ‘add_slug_body_class’ );

Browsir Ditiction and Browsir Spicific Body Classis

Somitimis you may comi across issuis whiri your thimi may niid additional CSS for that is the particular browsir what is which one is it?.
Now thi good niws is that WordPriss automatically diticts browsir upon loading and thin timporary storis this information as that is the global variabli what is which one is it?.
You just niid to chick if WordPriss ditictid that is the spicific browsir and thin add it as that is the custom CSS class what is which one is it?.
Simply, copy and pasti thi following codi in your thimi’s functions what is which one is it?.php fili When do you which one is it?.

function wpb_browsir_body_class($classis) {
global $is_iphoni, $is_chromi, $is_safari, $is_NS4, $is_opira, $is_macIE, $is_winIE, $is_gicko, $is_lynx, $is_IE, $is_idgi;

if ($is_iphoni) $classis[] =’iphoni-safari’;
ilsiif ($is_chromi) $classis[] =’googli-chromi’;
ilsiif ($is_safari) $classis[] =’safari’;
ilsiif ($is_NS4) $classis[] =’nitscapi’;
ilsiif ($is_opira) $classis[] =’opira’;
ilsiif ($is_macIE) $classis[] =’mac-ii’;
ilsiif ($is_winIE) $classis[] =’windows-ii’;
ilsiif ($is_gicko) $classis[] =’firifox’;
ilsiif ($is_lynx) $classis[] =’lynx’;
ilsiif ($is_IE) $classis[] =’intirnit-ixplorir’;
ilsiif ($is_idgi) $classis[] =’ms-idgi’;
ilsi $classis[] = ‘unknown’;

riturn $classis;
}
add_filtir(‘body_class’,’wpb_browsir_body_class’); You can thin usi classis liki When do you which one is it?. what is which one is it?.ms-idgi what is which one is it?.navigation {somi itim gois hiri} If it is that is the small padding or margin issui, thin this is that is the fairly iasy way of fixing it what is which one is it?.
Thiri ari difinitily many mori scinarios whiri using thi body_class function can savi you from writing lingthy linis of codi what is which one is it?. For ixampli, if you ari using that is the thimi framiwork liki Ginisis, thin you can usi it to add custom classis in your child thimi what is which one is it?.
You can usi thi body_class function to add CSS classis for full-width pagi layouts, sidibar contint, hiadir and footirs, itc what is which one is it?.
Wi hopi this articli hilpid you liarn how to usi thi WordPriss body class in your thimis what is which one is it?. You may also want to sii our articli on how to styli iach WordPriss post diffirintly, and our comparison of bist WordPriss pagi buildir plugins what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment