Grow your CSS skills. Land your dream job.

Problem Setting Body Class – is_page + in_category

  • # 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:

    Code:
    } elseif (is_page('Resume')) {
    echo 'resume';
    } elseif (is_page('Trips')) {
    echo 'trips';
    } elseif (is_page('Work')) {
    echo 'work';
    } elseif (is_page('About')) {
    echo 'about';
    } elseif (is_page('Contact')) {
    echo 'contact';
    } else {
    echo ' ';
    }
    ?>“>

    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:
    } elseif (is_page('Resume')) {
    echo 'resume';
    } elseif (is_page('Trips') || in_category('trips')) {
    echo 'trips';
    } elseif (is_page('Work') || in_category('work')) {
    echo 'work';
    } elseif (is_page('About')) {
    echo 'about';
    } elseif (is_page('Contact')) {
    echo 'contact';
    } else {
    echo '';
    }
    ?>“>

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

    # October 22, 2009 at 3:23 am

    Just use the body_class() function.
    http://www.nathanrice.net/blog/wordpress-2-8-and-the-body_class-function/

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

    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:13 pm

    Not really sure why you need to do all that.
    Never mind, you can create a body_class_plus() function http://digwp.com/2009/08/wordpress-body-class-plus/ or download the plugin http://digwp.com/2009/08/wordpress-body-class-plus/#downloads

    # 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 1:56 pm
    "marcott" wrote:
    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).

    Thats exactly what the body_class() function does, you don’t need all that extra stuff.

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

    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.

    # October 23, 2009 at 7:05 am

    Ok then, a quick google search turned this up http://wpkode.com/2009/06/a-better-body_class-for-wordpress-2-8/
    I haven’t tried it myself so please let me know if it’s what you are looking for.

    Code:
    ID) ) ); ?>>
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".