2

I added a custom field to a taxonomy term via 'Manage Fields' (see screenshot here: https://cl.ly/022v2B2H2c1m). How can I display this new field in the Frontend? In my template.php I have this:

$cats = taxonomy_get_tree(7);
$categories = array();
if (!empty($cats)) {
  foreach ($cats as $cat) {
    $parent = $cat->parents[0];
    $item = array(
      'tid' => $cat->tid,
      'name' => $cat->name,
      'description' => $cat->description,
      'image' => $cat->field_images,
      'children' => array(),
    );

  }
}

When I do a var_dump($item), the image field shows 'NULL', although I added an image.

Anybody with a solution?

Best wishes

1

You can use this -

$term = taxonomy_term_load($tid);
$field_image = field_get_items('taxonomy_term', $term, 'field_image');
if ($field_image) {
  $headerimg = field_view_value('taxonomy_term', $term, 'field_image', $field_image[0], array('type' => 'image'));
}
print render($headerimg);

In your file template.php use the theme_preprocess_page function like this :

function mytheme_preprocess_page(&$vars) {
  $term = menu_get_object('taxonomy_term', 2);
  if ($term) {
    $field_header_image = field_get_items('taxonomy_term', $term, 'field_header_image');
    if ($field_header_image) {
      $vars['headerimg'] = field_view_value('taxonomy_term', $term, 'field_header_image', $field_header_image[0], array('type' => 'image'));
    }
  }
}

Then in you page.tpl.php you can print render($headerimg);

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.