0

In my custom type "Page" I've added an image field (field_page_cover_image) and applied image style to it.

<?php
  $field = field_get_items('node', $node, 'field_page_cover_image');
  if($field){
     $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 have a requirement show this image (if uploaded) in all basic pages only (in a separate div container). For this I added the above code in `page.tpl.php

This code is working fine in all the pages but when I load search or other pages, I am getting error, as they don't have this field ($field).

1
  • Suresh there can be a relative easy approach for what you are trying to achieve, can you please add one more line in question about what exactly you want , in terms of functionality not in terms of code.
    – arpitr
    Dec 31, 2013 at 10:24

2 Answers 2

1

Solution 1 : use node.tpl.php

You should use a template for your specific content type "Page" instead of using page.tpl.php.

So try to copy paste the node.tpl.php, put your code in it, rename it node--page.tpl.php and upload it inside your theme folder. Then it should only apply to content type "Page", and not all pages.

Solution 2 : check that you're on a content type "Page" in page.tpl.php

If you need to put your code inside page.tpl.php, then you can check if you're viewing a content type "Page" this way:

$nid = arg(1);
$path = arg(0);
if(isset($nid) && is_numeric($nid) && isset($path) && $path == 'node') {
    $node = node_load($nid);
    if(isset($node) && $node->type == 'page') {
        // PUT YOUR CODE HERE
        ...
     }
}
3
  • Matt, the place where I want to show image is not into node.tpl.php. It is somewhere in header portion. So, I can't use node template :( Dec 31, 2013 at 10:29
  • So you can try the second solution :)
    – Matt
    Dec 31, 2013 at 10:30
  • <?php if ($node->type == 'page') {} ?> I added this condition to fix it. Thanks Dec 31, 2013 at 14:11
1

I suggest you to use template_preprocess_page and save your image to a variable. You can refer the below code.

function mytheme_preprocess_page(&$vars) {
     $node = $vars['node'];
     $field = field_get_items('node', $node, 'field_page_cover_image');
      if($field){
         $theme_img = file_create_url($node->field_page_cover_image[$node->language][0]['uri']); //origional image path
         if(isset($theme_img)) {
            //print styled image
            $imagevariable = theme('image_style', array('path' => $node->field_page_cover_image[$node->language][0]['uri'], 'style_name' => 'page_theme'));
           $vars['myImage'] = $imagevariable;
        }
      }  
}

You can use $myImage variable to print the image in the tpl file :)

3
  • You can't always assume that a node is available in a preprocess_page, this will throw warnings ... (try on a user page) Dec 31, 2013 at 7:12
  • @MikeVranckx , yes you right, I write this to take clue to him. the main idea is prepare variable that can use every where.
    – Yuseferi
    Dec 31, 2013 at 7:55
  • Hi, thanks for responding. The problem is with $field there is something wrong in that line. I am getting error in search, user and other pages, if I use it. Dec 31, 2013 at 9:18

Your Answer

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

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