Solved

Wordpress Body Class

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

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 56

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 56

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

680 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