I'm using Taxonomy Image, but it would be nice if this module allowed for the image that is assigned to a given taxonomy term to show at the top of that specific term (and subterms, if set to recurring) listing.

I've tried all day to make this happen in the theme itself with no luck. I'm finding that the taxonomy_image_display($term->tid) function isn't very powerful (unless it's only being used in the node.tpl.php file).

I added this as a feature request for Taxonomy Image module, but I'm wondering if anyone knows of a theming method of doing this...

Comments

Nick Lewis’s picture

Don't have enough time to look specifically into this matter, but this will probably set you on the right path.

So on a taxonomy term listing page, the URL will look like "http://www.example.com/taxonomy/term/4". The number 4 refers to the term_id. Thus in page.tpl.php, you can make term information available to you like a so:

// don't run this process unless you have the correct url paramaters
if (arg(0) == "taxonomy" && is_numeric(arg(2))) {
$tid = arg(2);
// now use the core taxonomy function to load term info
$terms = taxonomy_get_term($tid);
// that core function returns the object in array, so we have to do this extra step...
$term = $terms[$tid];
$image = taxonomy_image_display($term->tid);
print $image; 
}

--
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
--
Personal: http://www.nicklewis.org
Work: http://www.onnetworks.com

--
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
--
Personal: http://www.nicklewis.org
Work: http://www.zivtech.com

steingard’s picture

Thanks for helping me out. I had tried something similar as what you stated, but not as extensive. I didn't think I'd have to grab the taxonomy term manually. I figured it would be smart enough to know what term it was displaying on its own.

Anyway, it threw the following error.

Fatal error: Cannot use object of type stdClass as array in /var/www/drupal/sites/all/themes/theme1/page.tpl.php on line 76

heh.. I'm just using notepad at the moment to edit, so I manually counted the lines and 76 is the $image = taxonomy_image_display($term->tid); line. What's stdClass?

I also just tried to change
if (arg(0) == "taxonomy" && is_numeric(arg(2))) {
to be
if (arg(0) == "taxonomy" && arg(1) == "term") {
and... it does nothing at all. not even throwing an error.

I'll play a bit more, but I don't know enough advanced PHP to be useful in this kind of troubleshooting.

Thanks again

Nick Lewis’s picture

Dude, download this right now: http://notepad-plus.sourceforge.net/uk/site.htm

Notepad++ is a lightweight code editor that will make your programming life 15X easier.
+++
It looks like I made a bad assumption about how the data is structured. Try this before the offending line:

function pre_print($input) {
print '<pre>';
print_r($input);
print '</pre>';
}
pre_print($term);

This will actually output the structure of the $term variable on to the page. Always do this when you have an error, its the first step to debugging, and dymystying scary messages like "FATAL ERROR: YOUR FAMILY AND FRIENDS WILL DIE!"

Paste the result to me (if it exists), and I'll show you were I went wrong.

***

FYI, stdClass refers to a data type that looks like this in code "$node->example". This is as opposed to an array which would look like "$node['example']". The output of the function I wrote will let you know. If you read "stdClass" than it means you need to follow "$node->example". So theoretically, you might be able to fix this right now by rewriting line 76 as:

// notice this is written like an array, instead of $term->tid (stdClass a.k.a object)
$image = taxonomy_image_display($term['tid']); 

p.s. it was stuff like this that drove me nuts when I was first learning php.

--
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
--
Personal: http://www.nicklewis.org
Work: http://www.onnetworks.com

--
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
--
Personal: http://www.nicklewis.org
Work: http://www.zivtech.com

summit’s picture

bookmarking, greetings, Martijn