/ Published in: HTML
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * This snippet creates a <body> class and id for each page. * * - Class names are general, applying to a whole section of documents (e.g. admin or ). * - Id names are unique, applying to a single page. */ // Remove any leading and trailing slashes. $uri_path = trim($_SERVER['REQUEST_URI'], '/'); // Split up the remaining URI into an array, using '/' as delimiter. $uri_parts = explode('/', $uri_path); // If the first part is empty, label both id and class 'main'. if ($uri_parts[0] == '') { $body_id = 'main'; $body_class = 'main'; } else { // Construct the id name from the full URI, replacing slashes with dashes. $body_id = str_replace('/','-', $uri_path); // Construct the class name from the first part of the URI only. $body_class = $uri_parts[0]; } /** * Add prefixes to create a kind of protective namepace to prevent possible * conflict with other css selectors. * * - Prefix body ids with "page-"(since we use them to target a specific page). * - Prefix body classes with "section-"(since we use them to target a whole sections). */ $body_id = 'page-'.$body_id; $body_class = 'section-'.$body_class; print "<body class="$body_class" id="$body_id""; print theme('onload_attribute'); print ">"; ?>