Show block if inside specific book

Last modified: October 14, 2009 - 22:43

If you wish to display a block only while showing a page from level 1 or 2 of a specific book (note in Drupal 6 this code does not work below that level), try the following:

  1. Go to the block you want to edit and click configure
  2. Go to the "Page specific visibility settings"
  3. Choose the option: "Show if the following PHP code returns TRUE"
  4. Paste in the code below
  5. Change the two values of 46 to the node ID of the book you're testing against

Then the block should only display if you're inside that particular book.

Drupal 5

<?php
if (arg(0) == "node" && is_numeric(arg(1))) {
  if (
46 == arg(1)) {
    return
TRUE;
  }
 
$result = db_fetch_object(db_query("SELECT parent FROM {book} WHERE nid = %d", arg(1)));
  if (
46 == $result->parent) {
    return
TRUE;
  } else {
    return
FALSE;
  }
} else {
  return
FALSE;
}
?>

Drupal 6

In Drupal 6 the column 'parent' is now BID (book ID).

<?php
if (arg(0) == "node" && is_numeric(arg(1))) {
  if (
46 == arg(1)) {
    return
TRUE;
  }
 
$result = db_fetch_object(db_query("SELECT bid FROM {book} WHERE nid = %d", arg(1)));
  if (
46 == $result->bid) {
    return
TRUE;
  }
  else {
    return
FALSE;
  }
}
else {
  return
FALSE;
}
?>

'46' is the nid of your book and needs to be changed to the nid of the specific book you are attaching this block to.

These snippets are from help provided in this forum thread: http://drupal.org/node/125344. Thanks to all who helped!

 
 

Drupal is a registered trademark of Dries Buytaert.