0

This is a question in reference to Drupal 7, using the Zurb Foundation base theme framework. I've created a specific content type that only has an image field in it for purposes of making the image itself content manageable by the user so they can add/remove as they'd like.

I thought that I could access that image with the following code in my page--front.tpl.php file per my Googling of the subject, however, I'm getting no output at all.

<?php print render(field_view_field('node', $node, 'field_my_image_field', array('label'=>'hidden'))); 
  ?>

I've also attempted several different variations of this to no avail.

How can I access and print out the contents of that field from that content type into my custom page template?

0

You can embed a view inside your front page template, insert this code in propper region. You'll need an image view

// Get the embeded view and render it.
drupal_render(views_embed_view('VIEW_NAME', 'DISPLAY_ID'));

replace VIEW-NAME and DISPLAY_ID with your data, you can find those values in last column on your image view

0

if you want to display any custom field from specific content type , you can refer below code : please change your content type name in $type variable in below code here my content type name is "portfolio" , you will get all fields value from that particular content type.

$type = "portfolio";
//$test = array();
$nodes = node_load_multiple(array(), array('type' => $type));
foreach($nodes as $products){
 print_r($products); 
} 

Hope it helps.

  • What does the $products variable represent? I'm assuming that this has something to do with your image field, but I'm not following you here - it's not clear. – Rob Orr Jun 14 '17 at 14:52
  • Hello, here $products variable you can give any name , try to print that variable , you can give any variable name there like $test then it should look like foreach($nodes as $test){ print_r($test); } , try this code you will surly get your field , you will get your custom image field uri then you need to convert it in url using drupal's function like file_create_url , for example , $main_image = file_create_url($test->yourcustomimagefieldname['und'][0]['uri']); , so this function will create image url – megi Jun 15 '17 at 7:20
  • This returns an error: Error messageNotice: Undefined index: u‌​ri in include() (line 11 of sites/all/themes/my_theme/templates/page--front.tpl.php). – Rob Orr Jun 15 '17 at 18:21
  • try to do foreach($nodes as $test){ print_r($test); } and check if you are getting image field or not ? – megi Jun 19 '17 at 4:46
  • check in admin that your field has image uploaded or not,if there will be no image then on doing print_r you will not get anything for that field. in my project i have field like this prntscr.com/flfhxz , so when i will run my code it will do print_r like image this prntscr.com/flfijd because in admin my field has value . please share your print_r screenshot , because above mention code works perfect for me. – megi Jun 19 '17 at 5:19
0

Here is what was ultimately the solution in our case in the event that anyone else runs in to this same problem again in the future.

Ultimately, the solution was to load the image from the node directly. Many thanks to my man Mike for helping get to the bottom of this.

<?php 
      $type = "homepage_hero_background"; // content type machine name
      $nodes = node_load_multiple(array(), array('type' => $type));
      foreach ($nodes as $bg) {
        $uri = $bg->field_chp_background_image['und'][0]["uri"]; // field machine name
        $main_image = file_create_url( $uri );
      }
      ?>

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.