Solved

Wordpress Body Class

Posted on 2016-11-29
5
7 Views
Last Modified: 2016-11-30
I have the following in the header: <body <?php body_class(); ?> >
Some of the templates in the site fill that in, other pages don't now that I have changed the menu order.  How do I set in the template what the Body Class should be?
0
Comment
Question by:rgranlund
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Take a look at the body_class documentation here (https://developer.wordpress.org/reference/functions/body_class/)

You can add your own class in there like this (From the above link)
<body <?php body_class( 'class-name' ); ?>>

Open in new window


Add close to specific page template
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( is_page_template( 'page-example.php' ) ) {
        $classes[] = 'example';
    }
    return $classes;
}

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
What do you mean "Add close to specific page template"?  Place it at the end of the template?
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
No you put that in your functions.php file. The code hooks the body_class callback and then checks for a specified page and if found adds the custom class.

Provided as an example as to how you can add a custom class to specific pages.

EDIT Close meant to be Class;
0
 
LVL 7

Author Closing Comment

by:rgranlund
Comment Utility
I actually had to use is_page to make it work for some reason but it got the job done.  Thanks.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now