เพิ่ม id ให้กับ body ใน drupal


ผมหามานานครับสำหรับวิธีเพิ่ม id ให้กับ tag body ใน theme drupal ที่เราใช้งานอยู่ ถามว่าเพิ่มทำไม? มันมีประโยชน์สำหรับการเล่น css เพื่อออกแบบ theme ของเราครับ

โดยในกรณีนี้จะเอาประโยชน์ของ pathauto มาเป็น id ให้กับ body ใน theme ของเรากล่าวคือใน tag body นั้นทุกๆหน้าเราจะได้ id ที่ชื่อด้วย url alias ของเรา 

โดยให้เราไปแก้ไขไฟล์ page.tpl.php ใน theme ที่เราใช้งานอยู่โดยเพิ่มโค็ดด้านล่างก่อน tag body

<?php
  if(module_exists('pathauto')) {
    $result = db_query("SELECT dst FROM {url_alias} WHERE src='%s'", $_GET['q']);
    if ($data = db_fetch_object($result)) {
      $body_id = str_replace("/", "-", $data->dst);
    }
  }
  else {
      $body_id = str_replace(array('/', '_'), array('-', '-'), $_GET['q']);
  }
?>

ตัวอย่าง code

จากนั้นที่ tag body ให้แก้เป็นดังนี้

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

แค่นี้ก็เรียบร้อยแล้วในทุกๆหน้าเราจะได้ค่า id ใน body เป็น url ของหน้านั้นๆ

thank : http://www.elvisblogs.org/drupal/drupal-themes-different-body-tags-based-url


Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

CAPTCHA
คำถามนี้ทดสอบว่าคุณมีความรู้ทางคณิตศาสตร์ ไม่ได้โง่!!!
2 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.