Give help. Get help.

  • # October 22, 2009 at 3:06 am

    I 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:


    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:


    Please have a look and let me know if you can help me out.

    # October 22, 2009 at 12:27 pm

    I am still running into the same issue when using the body_class function. I am at a loss.

    Here is the code:

    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

    Well 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

    Ok, 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:


    Or when you are reading a post this is what the body tag looks like:


    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:


    I am sorry for being such a nuisance but I really am at a loss.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.