Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I used the Advanced Custom Fields plugin to add a custom field to my taxonomy. That custom field is an image which is associated with the term. Now I have a page where I display a list of all terms (for example, car manufacturers):

$terms = get_terms("manufacturer_tax", array(
    'hide_empty' => 0
));
$count = count($terms);
if ( $count > 0 ){
    foreach ( $terms as $term ) {
        echo $term->name;
        echo "<img src='" . $term->manufacturer_logo . "'>"; /* NOT WORKING */
     }
}

I wish to display the image associated with each term. How can I accomplish this?

EDIT

Here is the example result for one term: stdClass Object ( [term_id] => 5 [name] => Honda [slug] => honda [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => manufacturer_tax [description] => [parent] => 0 [count] => 0 )

Looks like there is no image associated with this term. However, I can see the image in the backoffice.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

OK had a go at this myself, I didn't realise ACF was able to add fields to taxonomies which is really handy so I wanted to figure it out too.

        <?php

        $libargs=array(  
            'hide_empty'        => 0,  
            'parent'        => 0,  
            'taxonomy'      => 'library_categories');  

            $libcats=get_categories($libargs);  

            foreach($libcats as $lc){ 
                $termlink = get_term_link( $lc->slug, 'library_categories' ); 

        ?>

            <a class="single-library-cat" href="<?php echo $termlink; ?>">
                <img src="<?php the_field('taxonomy_image', 'library_categories_'.$lc->term_id); ?>" />
                <?php echo $lc->name; ?>
            </a>

        <?php } ?>

It's in the docs here http://www.advancedcustomfields.com/docs/tutorials/retrieving-values-from-other-pages-taxonomy-user-media/

<?php the_field('taxonomy_image', 'library_categories_3'); ?>

So just replace the field name with your field name and library_categories_ with the taxonomy name. That should do it!

share|improve this answer
    
Thanks. Worked flawlessly. –  Cthulhu Nov 2 '12 at 12:26
add comment

Can you print the result of a $term so we can see what's stored?

I have used this plugin instead http://wordpress.org/extend/plugins/taxonomy-images/

and the code I've used to get the image for each taxonomy is:

<?php

        $libargs=array(  
            'hide_empty'        => 0,  
            'parent'        => 0,  
            'taxonomy'      => 'library_categories');  

            $libcats=get_categories($libargs);  

            foreach($libcats as $lc){ 
                $termlink = get_term_link( $lc->slug, 'library_categories' ); 
                $thumb_url = get_option('taxonomy_image_plugin');
                $thumb_url = wp_get_attachment_url( $thumb_url[$lc->term_taxonomy_id] );
            }
?>

But let's try to get it working with advanced custom fields first.

share|improve this answer
    
Thanks for your answer, I just updated my question with the result of a $term. I'm not sure if I could use the plugin Taxonomy Images, since I'm actually using two images for each term. –  Cthulhu Nov 2 '12 at 11:47
1  
Have you tried just using the $logo = get_the_field('manufacturer_logo'); –  Barry Walsh Nov 2 '12 at 11:52
    
Yeah that's definitely an easier way to get the custom field, as that's the inbuilt API call for the ACF plugin. –  userabuser Nov 2 '12 at 11:59
    
Yes, I tried get_field('manufacturer_logo'); and even the_field('manufacturer_logo');, and nothing happens. The image tag is there, but the source is empty (< img src="">). –  Cthulhu Nov 2 '12 at 12:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.