Drupal<body>にページ別にid、classを生成する。

in
印刷用ページ印刷用ページ

 Drupalで<body>にページごとのid、classを生成する方法。

参考はコチラ

まずはpage.tpl.phpの<body>を以下の内容に書き換えます。

 

 

 続いてtemplate.phpへ以下の内容を書き加えます。

/**
* Sets the body tag class and id attributes.
*
* From the Theme Developer's Guide, http://drupal.org/node/32077
*
* @param $is_front
*   boolean Whether or not the current page is the front page.
* @param $layout
*   string Which sidebars are being displayed.
* @return
*   string The rendered id and class attributes.
*/
function phptemplate_body_attributes($is_front = false, $layout = 'none') {

  if ($is_front) {
    $body_id = $body_class = 'homepage';
  }
  else {
    // Remove base path and any query string.
    global $base_path;
    list(,$path) = explode($base_path, $_SERVER['REQUEST_URI'], 2);
    list($path,) = explode('?', $path, 2);
    $path = rtrim($path, '/');
    // Construct the id name from the path, replacing slashes with dashes.
    $body_id = str_replace('/', '-', $path);
    // Construct the class name from the first part of the path only.
    list($body_class,) = explode('/', $path, 2);
  }
  $body_id = 'page-'. $body_id;
  $body_class = 'section-'. $body_class;

  // Use the same sidebar classes as Garland.
  $sidebar_class = ($layout == 'both') ? 'sidebars' : "sidebar-$layout";

  return " id=\"$body_id\" class=\"$body_class $sidebar_class\"";
}

 

 

こんな感じです。

 

 

 

以上で完了です!

 

変更前

 

 

変更後。<body>にid、classが生成されています。

Trackback URL for this post:

http://blogs.liquidst.jp/trackback/243