Fairly simple way to match sidebar content with the main content for several pages without the need to create multiple blocks or the hassle of putting content in a block.

For example, your Home, About and Contact pages all have a sidebar with some accompanying content which is different for each.

  1. Create pages (nodes) for each of the sidebar content and note the node ID.
  2. Add a block and paste in this code snippet
  3. Edit the Switch/Case part of this snippet for your nodes
  4. Enable the block for content/* and node/*
/**
 * This code snippet is used to show selected node content
 * in the block based on the current node id.  The idea is
 * to show relevant sidebar content that matches up with the
 * main content.  One block instead of several.
 *
 * If logged in user has admin nodes then show an Edit link.
 */
if ( arg(0) == "content" || arg(0) == "node" ) {
  //Choose which node to put into the block 
  switch (arg(1))  {
    case "10":  //nid of the home page
      $x_node = 58; //nid of content to load
      break;
    case "about":  //this page is a view made with Views module so no nid
      $x_node = 59;
      break;
    case "40":
      $x_node = 69;
      break;
    default:
      return FALSE;  
  }

  $node = node_load($x_node);
	
  //Add an edit link for admins
  if (user_access('administer nodes'))  {
    $nodeurl = url('node/'. $node->nid);
    print('<a href="'. $nodeurl .'">[edit]</a>');  
  }
	
  $nodeout = node_view($node);
  print $nodeout;
  
}

/*  // Helpful debugging info.
$x=arg(0);
$y=arg(1);
$z=arg(2);
print "<p>x=$x<br />y=$y<br />z=$z</p>";
// ==End Debug==  */

Comments

Caderial’s picture

This is a Great way to achieve this but i was wondering if there is a way to reference the Node by its Path aka URL instead of the node ID # ?

bydga’s picture

It would be dangerous to link to node by its alias. Imagine situation that the alias of the node changes. It would break the whole snipplet. Using the nid is the safest way.

rusdvl’s picture

where is this snippet suppose to go?

nargonne’s picture

Create/Add a new block and put this code there.

nerissa’s picture

I was wondering if anyone new how to code this?

I would like to add a blog page so it is viewable on the front page (such as I would probably need to use this code for that - I'm assuming) while keeping it on it's own blog page (of course).

So the view of this blog on the Front page would act like a mirror to the original blog. Hope I'm explaining this ok.

Please let me know if anyone knows how this can be done.

Thank you.

nargonne’s picture

Default is blog teasers are published to front page in reverse cron order. Enable blog module and start blogging. Fiddle with post settings and/or the break tag - <--break--> to adjust the amount of text shown in the teasers.

Is this what you are want?

kmiller-2’s picture

I created a block and copied the PHP snippet into the body with the hopes to connect specific Polls to specific Pages, but the block just displays all the PHP code. This is the first time I have used PHP code. What am I doing wrong?

Thanks

kmiller-2’s picture

my bad, needed to turn on PHP filtering. All good now.

technologie-life’s picture

hello everybody first im sorry for my english because im from Morocco any way first i search for how to add a node in a block
and i found these topic i didin't understande these code in the first time because hes cible for the develloper objectif so here is a general code you must just to add these code in the block and dont forget to chose the php filter of course , i really wait for any reply if you find these code util a will post more simple code and astuce for defferent topics let go to work!!

$xNode = ID; /* the node id for example 3 */

$node = node_load($xNode);
if (user_access('administer nodes'))
{
$nodeurl = url('node/'. $node->nid);
print('<a href="'.$nodeurl.'">[edit]</a>');
}
$nodeout = node_view($node);
print $nodeout;

and that's all

salamo 3alaykom

doru’s picture

Hi,

And thanks for posting these snippets.

What if we want the teaser content only?
I don't want the whole node's content to be shown, I only want the teaser to appear.
What would be the modiffication to the above.

Thanks,

trak-dev’s picture

This works great for nodes, thank you.

I am not getting to work with views, I modified it slightly to work with flash nodes, I have a view with the path /news when I use case"news"; I does not load?

I am using this with Drupal 6.

thank you,

eiland’s picture

To surpress the node title in de block, you need to change node.tpl.php

The line which read <?php if ((!$page): ?> needs to be

<?php if ((!$page) && ($title!="&lt;none&gt;")) : /* surpress node title in custom block */ ?>

eiland’s picture

I assume it is possible to achieve the same with panels. I have to figure out how; Which option would be the better one for n00b administrators?

cluke009’s picture

I could not get this working under Drupal 6.x but I came up with some new code that did the trick.

You can also remove the node title without modifying your node.tpl.php by uncommenting $node -> title = NULL;

<?php
/**
  * This code snippet is used to show selected node content
  * in the block based on the current node id.  The idea is
  * to show relevant sidebar content that matches up with the
  * main content.  One block instead of several.
  *
  * If logged in user has admin nodes then show an Edit link.
  */
  
  // Get current page url
  $current_url = $_SERVER[ 'REQUEST_URI' ];
  
  //Choose which node to put into the block
  switch( $current_url ) {
    case '/'://home page
      $x_node = 181;//nid of content to load
      break;
    case '/about'://url of about page
      $x_node = 185;
      break;
    case '/another-page':
      $x_node = 184;
      break;
    default: 
      return FALSE;
  }
  
  $node = node_load( $x_node );
  
  // Uncomment to remove node title
  // $node -> title = NULL;
  
  //Add an edit link for admins
  if( user_access( 'administer nodes' ) ){
    $nodeurl = url( 'node/' . $node -> nid );
    print( '<a href = "'. $nodeurl .'">[ 'edit' ]</a>' );
  }
  
  $nodeout = node_view( $node );
  print $nodeout;
  
  // Helpful debugging info.
  // print $current_url ;
?>
nargonne’s picture

I came back here to grab the snippet for a project and found this improvement for D6 works great.
One minor edit though:

Removed the single quote surrounding the word 'edit'.

print('<a href="'. $nodeurl .'">[ edit ]</a>');

bzsim’s picture

Will this work if I want a view to display in a block instead of nodes? I built a view that displays upcoming events, but I have tagged them using Taxonomy Term so that I can filter them and display them on certain pages. So on the Apples page, the view displays my events I tagged as "Apples", etc. One view, but it changes depending on the page the person is looking at.

aneeshaider’s picture

Hey,

Thank you for this useful tip. You have saved my day :)

bellerophons_pegasus’s picture

In case the url to a node contains more characters after the number, you may want to use regular expressions.

 // Get current page url
  $current_url = $_SERVER[ 'REQUEST_URI' ];
 
//Find 'node/NR' in e.g. drupalsandbox/?q=node/2#someanchor
//the regular expression can be used with or without clean urls activated
  preg_match( '/\/{1}\?q=?(node\/[0-9]+)/', $current_url, $matches);
  $current_node = $matches[1];

  //Choose which node to put into the block
  //With all other URLs (Nodes)
 switch( $current_node ) {
    case ('node/2'):
      $x_node = 41;
      break;
    case ('node/3'):
      $x_node = 42;
      break;
    default:
      return FALSE;
  }