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

I've loaded the above code in page.tpl.php. It is to load the uploaded styled image from individual pages.
I am getting error in line no. 6 ($theme_img), for pages which have empty image field. It is working fine in pages which have image uploaded. How to know if this field is empty or have image uploaded.

  • you should check for empty image field like this: !empty($node->field_page_cover_image).. – xurshid29 Jan 5 '14 at 9:05
2

Try this:

if ($node = menu_get_object('node')) {

  // Set default language if not set
  $node->language = isset($node->language) ? $node->language : LANGUAGE_NONE;

  if ($node->type == 'page' && isset($node->field_page_cover_image[$node->language][0]['uri'])) {
    print theme('image_style', 
            array('path' => $node->field_page_cover_image[$node->language][0]['uri'], 
                  'style_name' => 'page_theme'));
  }

}

Odd you're printing out the image though, must be in a tpl file. I'd suggest moving the code logic to a hook_preprocess_node and setting a template variable instead.

e.g:

/**
 * Implements hook_preprocess_node
 */
function MYTHEME_preprocess_node($variables){

  $node =& $variables['node']; // by reference
  // Set default language if not set
  $node->language = isset($node->language) ? $node->language : LANGUAGE_NONE;

  if ($node->type == 'page' && isset($node->field_page_cover_image[$node->language][0]['uri'])) {
    // Add new template variable called $page_theme_image
    $variables['page_theme_image'] = theme('image_style', 
            array('path' => $node->field_page_cover_image[$node->language][0]['uri'], 
                  'style_name' => 'page_theme'));
  }

}

and then in your tpl.php file, you just go:

if (!empty($page_theme_image)) {
  print $page_theme_image;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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