Hi There,

I'm creating an Acquia Marina sub-theme (will post to the group when finished) - I'm a little nervous when it comes to tweaking the template.php, but can anyone suggest a quick and simple way to print the page title in the body class? Like the way Zen Theme does. I'm wanting to have a different background image in the header of each page in primary links.

Brock

Comments

jwolf’s picture

Status: Active » Fixed

If I understand you correctly, you want an unique identifier for each page?

You can customize template.php, adding to function phptemplate_preprocess_page(), which adds a body_id to the <body> that will grab the current page's URL. In other words, give <body> tag a unique id depending on page path:

<?php
  // give <body> tag a unique id depending on PAGE PATH
  $uri_path = trim($_SERVER['REQUEST_URI'], '/');
  $uri_bits = explode('/', $uri_path);
  if ($uri_bits[0] == '') {
    $body_id = 'front';
    } else {
    $body_id = str_replace('/','-', $uri_path); // use dashes to replace slashes in the URI
    }
  $body_id = 'page-'.$body_id; // add 'page-' to the front of the id
  $vars['body_id'] = $body_id;
?>

Then you can add the $body_id variable to the <body> as follows:

<body id="<?php print $body_id ?>" class="<?php print $body_classes; ?>">

The following example is directed towards node/34
Within style.css you can target just the page that you want to add additional styles for just the node/34 page:

body#page-node-34 {
  background-color: #333;
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

gintass’s picture

Suggested code didn't work in my case since I wanted to apply unique CSS to a view with the pager. Pager would add an extra stuff to body ID. For example first page in this view would have id="page_publications" but the page 2 would have an id="page_publications?page=2" Since I wanted all pages in this view to have the same ID I modified the code above by using preg_replace instead of str_replace. This is because with preg_replace I could use regular expressions and remove '?page=1', '?page=2' etc. from body ID.

  $uri_path = trim($_SERVER['REQUEST_URI'], '/');
  $uri_bits = explode('/', $uri_path);
  if ($uri_bits[0] == '') {
    $body_id = 'front';
    } else {
    $find = array ('/\//', '/\?page=./'); //array with patterns to be replaced
    $replace = array ('-', ''); //this is what I'm replacing with
    $body_id = preg_replace($find2, $replace2, $uri_path); //I'm replacing slashes with dashes and removing '?page=3'
    }
  $body_id = 'page-'.$body_id; // add 'page-' to the front of the id
  $vars['body_id'] = $body_id;
sokrplare’s picture

Ran into some issues - I think because we're using Clean URLs where the code in #3 wouldn't work, but jwolf's code in #1 worked like a charm - thanks!