Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Problem Setting Body Class – is_page + in_category

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26525
    marcott
    Member

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

    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.

    #65820
    marcott
    Member

    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;
    }
    #65831
    marcott
    Member

    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).

    #65834
    marcott
    Member

    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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.