Solved

Wordpress Body Class

Posted on 2016-11-29
5
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 57

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 57

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 57

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to dynamically set the form action using jQuery.

717 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