WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Need to call parent page’s slug as body class

[Resolved] Need to call parent page’s slug as body class

  • iversoncreative
    Member

    @iversoncreative

    Right now I have the code below pulling up the page slug as the body class to control individual page’s style, but once you get to a subpage it doesn’t carry the page parent’s style, which is the goal. I could add the subpage slugs to my css, but if new subpages are added I’ll just have to add them to the css too, which is lame and chunks up and uglyfies my css. Any ideas here? Any info is greatly appreciated. Thanks!

    <body <?php if (is_front_page()) { ?>
    id="home"<?php } else { ?>
    id="interior" class="<?php echo $post->post_name; ?>"<?php } ?>>
Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi
    Forum Moderator

    @esmi

    Try:

    <body <?php if (is_front_page()) { ?>id="home"<?php }
    elseif (is_page() { ?>id="page_class"<?php }
    else {?>id="interior" class="<?php echo $post->post_name; ?>"<?php } ?>>
    iversoncreative
    Member

    @iversoncreative

    I got a syntax error?

    esmi
    Forum Moderator

    @esmi

    Attempt 2:

    <body <?php if (is_front_page()) { ?>id="home"<?php }
    else { ?>id="interior" class="<?php echo str_replace(' ','-',wp_title('',false,'')); ?>"<?php } ?>>
    iversoncreative
    Member

    @iversoncreative

    Nope, that’s just giving me the name of the current page as a class instead of the slug, which I’ve already got going on.

    Basically I want it to assign the body tag an id of either home if it’s the front page, or interior if it’s a interior page.

    Then I want it to assign the body tag a class of the page slug if it’s a top level page, the parent’s slug if it’s a second level page or the ancestor’s slug if it’s a third level page.

    It would make this aspect of my life so much easier right now and I have to figure it out, but I’m not very good at PHP and can barely figure out how to hack things to get them to work the way I need. It’s trial and error really at this point. Argh!

    iversoncreative
    Member

    @iversoncreative

    THIS GOT IT!!!

    WordPress 2.8 and the body_class() Function

    Thank you WordPress 2.8!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Need to call parent page’s slug as body class’ is closed to new replies.