Solved

Wordpress Body Class

Posted on 2016-11-29
5
34 Views
Last Modified: 2017-01-25
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 55

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 55

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 55

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

790 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