I'm trying to change the logo in my header based on the taxonomy term of a page. I have managed to get it working on the taxonomy landing page itself. However, I have content pages that have term references on them which don't pull through the term title.

This the code that allowing me to display the title on the taxonomy page:

$termid = arg(2);
      $term = taxonomy_term_load($termid);
      $title = $term->name;

      $title = str_replace(' ', '', $title);


I am then stripping out the whitespace and printing the value inside an img tag.

I've tried doing this with views and creating a list of all nodes and adding a contextual filter. Yet this seems to only work the other way. So it only displays the image on the node page itself and NOT the taxonomy landing page.

Any help would be much appreciated.


1 Answer 1


You can make the code work in 2 situations.

$term = menu_get_object('taxonomy_term', 2);
if (empty($term)) {
  if ($node = menu_get_object('node', 1) ) {
    $wrapper = entity_metadata_wrapper('node', $node);
    $term = $wrapper->field_organisation->value();

if ($term) {
  $title = $term->name;
  $title = str_replace(' ', '', $title);

entity_metadata_wrapper() is the function provided by module Entity

  • Sorry, something has just come up. It appears that when I am logged in as an admin if works fine. However when i logout and visit the URL /user i seem to get the following error in the logs: PHP Fatal error: Call to a member function value() on a non-object in
    – JDavies
    Commented Aug 1, 2016 at 10:22
  • I have just updated the code. Please try.
    – Jimmy Ko
    Commented Aug 1, 2016 at 10:27
  • Glad to know it :)
    – Jimmy Ko
    Commented Aug 1, 2016 at 10:37

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.