I have a nodes, and a few vocabularies to choose terms from. I would like to show images of terms from specific vocabulary in nodes (If node is associated with term from specific vocabulary, it would show image from that terms field). What would be the best (most "Drupal") approach to this? :)
One way to do it -- if you don't want to create/mess with the theme template files for your content type, would be to display the corresponding tag image via a view.
Here are the steps to doing that:
- I'm assuming you already have your vocabulary set up properly and you have the image field added to it (can be done at admin/structure/taxonomy/[YOUR VOCAB ID]/fields) and some terms have been populated with their corresponding images.
- Create a new view with a block display, and set it to apply only to the relevant content types
- Add a term relationship to your view, so you can access the vocab/term fields, and not just the node fields!!
- Once you've done that, go into the fields you want to pull for each node. I'd remove the 'Title' field that's added by default and only add the field that represents the image associated with your term. It should be somewhere under the 'Taxonomy term:....' section of the field list.
- Make sure that you set it to use the relationship you created above
- Finally, since we'll want this block to take its cue of which image to display based on which node it's on, let's add a contextual filter. Under the 'Advanced' section --> 'Contextual filters', add the 'Content: Nid' one and make sure you set it up to take its NID from the page it appears on if a filter isn't present in the URL
- All done! Go to one of the relevant pages and test. Of course, you'll have to enable the block via contexts or the block admin page (admin/structure/block) first, since it's not placed anywhere by default. Display the block on the second sidebar, in the actual content section -- anywhere you want.
Let me know if this helps!!
================ EDIT ===================== Solution 2 (upon request)
To do this through your template file, go ahead and copy your regular node.tlp.php file and rename it node--[YOUR CONTENT TYPE MACHINE NAME].tpl.php
Then, right under or above the content, insert the following code:
$term=taxonomy_term_load($node->field_thats_term_reference['und']['tid']); $image_field=field_view_field('taxonomy_term', $term,'field_main_image'); print render($image_field);
Just substitute 'field_thats_term_reference' with the name of your term reference field, and substitute 'field_main_image' with your image field name.
That'll do the print out. You can place this anywhere you want. If you want to customize how the print out actually looks, just do a dpm($image_field) once you've set that variable (must have the devel module installed), and you can see all the parts of that field and how to get to them if you want to print them out separately as opposed to the default way render() prints it all out. I think that part is beyond the scope of this question, but it should get you started!