WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Taxonomy Images] Get image on single page

[Plugin: Taxonomy Images] Get image on single page

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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' );
     }

    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?

    @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.

    Thanks guys, this helped me a lot!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Taxonomy Images] Get image on single page’ is closed to new replies.