Specifically, I'm trying to access a field called 'logo' inside of a couple taxonomy terms (Tweet and Wikipedia edit), which is an image. I have a news feed on the front page of my site, and for each feed item that has a term reference to one of these image beside each feed item in the feed. What would be the best way to go about exposing or accessing the logo field from each taxonomy term?
2 Answers
If you are going to be accessing the content of what drupal call entities (data objects, like nodes, users, taxonomies etc), you might considder using the entity module. It adds some nice helper functions. What Nikit writes is not possible, you would do something like this:
$term_id_1 = $node->field_term['und'][0]['tid'];
$term_id_2 = $node->field_term['und'][1]['tid'];
Then you would have to load the terms etc. Instead with the entity module you can do.
$images = array();
$node_wrapper = entity_metadata_wrapper('node', $node);
foreach ($node_wrapper->field_term as $term_wrapper) {
$images[] = $term_wrapper->field_image->value();
}
$images
will now be an array with the image field object that holds info like uri, fid etc.
Using taxonomy_term_load
and taxonomy_term_load_multiple
will provide you with the fully loaded term object including any custom fields.
Alternatively, use taxonomy_get_tree
with the final parameter ($load_entities
) set to TRUE
.
-
1Thanks @Joshua_Stewardson for that mention of the final parameter of taxonomy_get_tree! Lifesaver. Commented Feb 25, 2013 at 20:16