1

I can print a field in content type tpl file with this:

<?php echo render($content['field_name']); ?>

But how can I print the field as a different display mode?

EG I might always want to print the teaser version of an image field, regardless of what view mode is being used.

1 Answer 1

0

There might be a shortcut to this but I don't know it...

// Get the bundle settings.
$info = field_info_instance('node', 'field_name', 'content_type');

// Get the field render array
$build = field_view_field('node', $node, 'field_name', $info['display']['teaser']);

// And print it
print render($build);

Moving the display building logic to a preprocess function would probably be a good idea.

5
  • Sorry but you've misunderstood my question. I've edited it to make it clearer.
    – Evanss
    Commented May 13, 2013 at 14:51
  • @jdln I've had another go :)
    – Clive
    Commented May 13, 2013 at 14:58
  • Its not printing anything for me. The only bits ive changed are 'field_name','content_type' and 'teaser', is that correct?
    – Evanss
    Commented May 13, 2013 at 15:06
  • Yep that's correct. I just tried the code on a site of mine and it works fine. Have you replaced field_name in both places?
    – Clive
    Commented May 13, 2013 at 15:07
  • I was expecting my display mode to be 'my_display_mode' but actually the machine name is 'mydisplaymode'. Working fine, thanks.
    – Evanss
    Commented May 13, 2013 at 15:22

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.