WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need to call parent page's slug as body class (6 posts)

  1. iversoncreative
    Member
    Posted 5 years ago #

    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 } ?>>
  2. esmi
    Forum Moderator
    Posted 5 years ago #

    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 } ?>>
  3. iversoncreative
    Member
    Posted 5 years ago #

    I got a syntax error?

  4. esmi
    Forum Moderator
    Posted 5 years ago #

    Attempt 2:

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

    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!

  6. iversoncreative
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic