WordPress.org

Forums

Taxonomy Images
Get image on single page (5 posts)

  1. stevejonesdev
    Member
    Posted 3 years ago #

    I'm having trouble getting this to work.
    On a single page I want to get the category image of a particular category. I already have the category name I just need to know how to fetch the category image.

    Any help would be greatly appreciated.

    http://wordpress.org/extend/plugins/taxonomy-images/

  2. stevejonesdev
    Member
    Posted 3 years ago #

    Okay I figured it out. Here the code.

    $categories =  get_categories(array( 'taxonomy' => 'series' ));
     foreach ($categories as $category) {
     	$tax_term_id = $category->term_taxonomy_id;
     	$images = get_option('taxonomy_image_plugin');
     	echo wp_get_attachment_image( $images[$tax_term_id], 'medium' );
     }
  3. Shutez
    Member
    Posted 3 years ago #

    I'm using that on single.php, HOWEVER, I'm getting one image for every term in my taxonomy.

    Any way to make it so you just get the term that applies to the current page?

  4. MRWweb
    Member
    Posted 3 years ago #

    @shutez,

    If you know the ID of the term you want—using something like wp_get_object_terms()—you can just use the following code (based on the above snippet from @stevejonesdev) where $tax_term_id is the ID of the term image you want.

    $images = get_option('taxonomy_image_plugin');
    echo wp_get_attachment_image( $images[$tax_term_id], 'medium' );

    For others who stumble across this thread, I also thought I'd add this snippet which checks to make sure that the term has an image at all. Otherwise, WordPress throws an error (at least with wp_debug set to TRUE).

    $terms = wp_get_object_terms( $post->ID, 'TAXONOMY NAME', array('fields' => 'all') ); // get all the terms on that post
    $images = get_option('taxonomy_image_plugin'); // get the taxonomy images array
    foreach($terms as $term) { // iterate through each term
        $term_id = $term->term_taxonomy_id; // get the ID of the term
        if( array_key_exists( $term_id, $images ) ) { // check if term has an image
            echo wp_get_attachment_image( $images[$term_id] ); // if it does, echo that image
        }
    }

    Note to @stevejonesdev: I moved $images outside of the foreach loop. It seems to work the same and I imagine this means a few less queries run on the database.

  5. MichelleLeslie
    Member
    Posted 3 years ago #

    Thanks guys, this helped me a lot!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Taxonomy Images
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic