• As the Topic title suggests, I’m wondering how to take the name that I give whatever new page i create in wordpress and assign that name as the value of class attribute for the <body> tag…

    Any takers?

Viewing 3 replies - 1 through 3 (of 3 total)
  • for instance, adding ‘page-‘ before the page slug (using the page slug rather than the page title as this could contain spaces and other non-css characters):

    <?php if(is_page()) { $page_name = 'page-'.$post->post_name; } ?>
    
    <body <?php body_class($page_name); ?>>

    http://codex.wordpress.org/Function_Reference/body_class

    Thread Starter ThatGuyLeon

    (@thatguyleon)

    Thanks for the help. I’ve added that to header.php which I assume was the thing to do (I’m new to this). I’ve created a new page (named “page leon”) and upon viewing the page’s source code I get the following: <body class=”page page-id-14 page-template-default page-page-leon pagename-page-leon”>

    So, would I be correct in guessing that I can (or more importantly, should) use either “page-id-14”, “page-page-leon”, or “pagename-page-leon”? — I’ve tried using the named options as class selectors in the css and it works!..I’m just curious–being new to this– as to whether this is the best way to go or not.

    Thanks very much again for your help…the progress I’ve made thanks to you is far beyond what I would have accomplished without.

    Kind rehards

    you are doing it right 😉

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to apply the Name of a page as the value for the class attribute of’ is closed to new replies.