Using Taxonomy Image code with my get_categories code

I’m using Taxonomy Images to associate images with categories. I’m using the following code, one is to display the categories, the other to display the images.

Here is the code that displays my categories.

Read More
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";

foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
    echo '<p>'. $categories_item->description . '</p>';
}
echo "</ul>";
}
?>

Here is the code that displays the images.

$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {

    foreach( (array) $terms as $term ) {
        print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' );
    }

}

If i put the image code above the foreach for the categories it will show the images above my categories. If i put the image code below the end of the foreach for the categories it will show the images below the categories.

If i put the image code inside the foreach for the category it will show the all of the images under every category. So under the first category it will show the image for all categories and it will do the same under all of the other categories.

How can i combine both pieces of code so it will display the category, the image associated with that category then the second category and the image associated with that category?

I’ve tried the answer below but the image code doesn’t work without the foreach $terms as $term, so i’m still stuck trying to do this.

Related posts

Leave a Reply

4 comments

  1. No luck by using plugin provided functions as it gets it’s id by

    $obj = get_queried_object();
    

    To get the image I use this code snippet:

    $associations = taxonomy_image_plugin_get_associations();
    $tt_id = absint( $taxonomy_term->term_id );
    
    if ( array_key_exists( $tt_id, $associations ) ) {
        $image_id = absint( $associations[ $tt_id ] );
    }
    
    $image = wp_get_attachment_image( $image_id, 'thumbnail' );
    
  2. Just got this work out today by the code below.

    
    $imgs = get_option( 'taxonomy_image_plugin' );
    $categories = get_categories(array('orderby' => 'id', 'hide_empty' => 0));
    
    foreach ( $categories as $category ) {
        $id = $category->term_id;
        echo wp_get_attachment_image( $imgs[$id], 'thumbnail' );
    }
    
    

    I use get_option( ‘taxonomy_image_plugin’ ) to get an association between term_id and attachment_id, after that everything works like a charm.

  3. As described via the plugins wordpress download page instructions:

    $image_id = apply_filters( 'taxonomy-images-queried-term-image-id', $categories_item->term_id );
    wp_get_attachment_image( $image_id, 'detail' );
    

    e.g.

    <?php 
    $cat_id = get_query_var('cat');
    $catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
    echo "<ul>";
    foreach($catlist as $categories_item)
    {
        $image_id = apply_filters( 'taxonomy-images-queried-term-image-id', $categories_item->term_id );
        print '<li><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' .wp_get_attachment_image( $image_id, 'detail' ).'</a></li>';
    }
    echo "</ul>";
    ?>
    
  4. Got this working by using the code below. It will show the categories and the image associated with it.

        <?php 
    $cat_id = get_query_var('cat');
    $catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
    echo "<ul>";
    
    foreach($catlist as $categories_item)
    {
    echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
    
    echo '<div class="categoryoverview clearfix">';
        $terms = apply_filters( 'taxonomy-images-get-terms', '' );
        if ( ! empty( $terms ) ) {
    
          foreach( (array) $terms as $term ) {
            if($term->term_id == $categories_item->term_id) {
               print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
               echo '</a>';
    
            }
        }
        echo '<p>'. $categories_item->description; echo '</p>';
    }
    echo '</div>';
    }
    echo "</ul>";