0
<?php 
  $nid = arg(1);
  $arg0 = arg(0);
  if(isset($nid) && is_numeric($nid) && isset($arg0) && $arg0 == 'node') {
    $node = node_load($nid);
  }
  $theme_img = file_create_url($node->field_page_cover_image[$node->language][0]['uri']); //origional image path
  if(isset($theme_img)) {
    // print styled image
    print theme('image_style', array('path' => $node->field_page_cover_image[$node->language][0]['uri'], 'style_name' => 'page_theme')); 
  }  
?>

I am using the above code to load styled image (from node 6) in page.tpl file. This is working fine in all the pages but when I am trying to load images from individual images by using '$nid' it is not working.
I don't understand what I made mistake in code due to which I couldn't get the value of '$nid'. Please help.

1
  • Why are expecting $nid as a global variable or if not your above code is not showing how you are using $nid.
    – arpitr
    Commented Dec 30, 2013 at 6:14

3 Answers 3

0

I guess you want to replace the "6" by the current nid of the page. If so, you could use this code to get the nid of the current page:

$nid = arg(1);
$arg0 = arg(0)
if(isset($nid) && is_numeric($nid) && isset($arg0) && $arg0 == 'node') {
  //If you're using devel
  dsm($nid);
  //If not
  drupal_set_message($nid);
}

But as stated by Jeet, if the code is specific to one content type, you should do this in a node--mycontenttype.tpl.php and not in the page.tpl.php

2
  • Thanks Matt. Due to your code I could get the node ID but getting some error on node page which doesn't have field "field_page_cover_image". Please see the updated code above. Commented Dec 30, 2013 at 11:49
  • The modified code works fine in pages which has images uploaded. It doesn't work on pages which doesn't have image field or uploaded image. Commented Dec 30, 2013 at 11:54
0

As you are looking to edit/override node template, it is suggested to place your changes into node.tpl.php where you will get $nid as out of the box. page.tpl.php will not give you node variables by default.

If you still you want to get your code on page.tpl.php you can get $nid from url like arg(1).

Let me know if I miss anything!

0

If you want to use $nid variable in your page.tpl.php, you can add that variable to be available in the page.tpl.php by implementing hook_preprocess_page() in your theme's template.php. In your implementation of the hook_preprocess_page() you have to add a key 'nid' to the $variables array. Eg: $variables['nid'] = the_nid_you_want;. After you have saved the changes, clear the Drupal cache and the $nid variable will now be available in your page.tpl.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.