Solved

Wordpress Body Class

Posted on 2016-11-29
5
29 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 52

Expert Comment

by:Julian Hansen
ID: 41906794
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
ID: 41907756
What do you mean "Add close to specific page template"?  Place it at the end of the template?
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41907773
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
ID: 41907826
I actually had to use is_page to make it work for some reason but it got the job done.  Thanks.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41907905
You are welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

910 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

21 Experts available now in Live!

Get 1:1 Help Now