Add node content into block depending on page ID

Last modified: April 16, 2009 - 19:12

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/*

<?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.
*/
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==  */
?>

Great Way ta do it But...?

Caderial - January 15, 2009 - 18:33

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 # ?

It would be dangerous to

bydga - February 8, 2009 - 10:54

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.

where is this snippet

rusdvl - February 17, 2009 - 22:13

where is this snippet suppose to go?

It goes in a block

nargonne - March 10, 2009 - 00:41

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

Want to use this code and have it only post to front page

nerissa - March 19, 2009 - 23:05

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.

Publish to front page

nargonne - March 20, 2009 - 13:05

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?

need help getting this to work.

kmiller - April 9, 2009 - 18:16

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

got it to work...

kmiller - April 9, 2009 - 18:25

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

Best Solution

technologie-life - May 18, 2009 - 12:06

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!!

<?php

$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

Views

traskconsultancy - May 28, 2009 - 21:17

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,

 
 

Drupal is a registered trademark of Dries Buytaert.