- This topic is empty.
-
AuthorPosts
-
October 22, 2009 at 3:06 am #26525
marcott
MemberI am trying to set to set the body’s class based on either the page name or the posts category. If I use the following conditional it works fine for setting the pages body class:
Code:“>But when I introduce the in_category to the mix it stops working properly – to be more specific the conditionals before the use of the in_category work fine but everything after do not. Here is the code:
Code:“>Please have a look and let me know if you can help me out.
October 22, 2009 at 12:27 pm #65820marcott
MemberI am still running into the same issue when using the body_class function. I am at a loss.
Here is the code:
Code:add_filter(‘body_class’,’my_body_classes’);
function my_body_classes($classes, $class) {
if (is_home()) $classes[] = ‘home’;
elseif (is_page(‘resume’)) $classes[] = ‘resume’;
elseif (is_page(‘trips’)) $classes[] = ‘trips’;
elseif (is_page(‘work’)) $classes[] = ‘work’;
elseif (is_page(‘about’)) $classes[] = ‘about’;
elseif (is_page(‘contact’)) $classes[] = ‘contact’;
elseif (in_category(‘trips’)) $classes[] = ‘trips’;
elseif (in_category(‘work’)) $classes[] = ‘work’;
return $classes;
}October 22, 2009 at 1:29 pm #65831marcott
MemberWell my problem is that I want the body’s class to be set by the page slug when the user is on a page. When the user is viewing a post I would like the body’s class to be the posts category. I other words if I am on the front page the body class would be ‘home’ or if I am ready a post under the trips category the body’s class would be ‘trips’.
Setting the class using the pages slug works fine. When I try to set it using the category things get a little weird (as described in my initial post).
October 22, 2009 at 2:36 pm #65834marcott
MemberOk, I understand what the body_class does but how do I get it to print the pages slug to a pages body class or the category’s name to the posts body class?
Right now when you view a page this is what the body tag looks like:
Code:Or when you are reading a post this is what the body tag looks like:
Code:I need the pages slug and or category name to be included in the class so I can target them with specific CSS. For example, if I am reading a post under the trips category the body tag would look like this:
Code:I am sorry for being such a nuisance but I really am at a loss.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.