1

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.

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

4 Answers 4

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' );
1
  • Awesome @Hrvoje, that's my exact problem too. The plug in states that it only works on pages like category.php/taxonomy.php, etc. I want to use it on an archive page and it wasn't working for this exact reason. Your code did the trick. +1
    – Jeff
    Feb 9, 2012 at 18:48
1

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.

0

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>";
?>
3
  • Thanks but i'm unsure where this is meant to go. Is this code instead of the $term code i posted? Where should i put it in my code that show the categories so it shows like <h1><p> then the image, showing every category image after the correct category. Instead of showing the images in their own list.
    – Corbula
    Nov 15, 2011 at 19:46
  • I've modified the answer to show an example that would give the same output as the code you specified in your question that works, but using the categories. You can adapt it to whatever you need as it's demonstrative of what you need to do to accomplish it. I can't give you an exact example for what your doing as I'm unsure of the way you want the markup to show, and it'd be doing your work for you, but I have demonstrated the step you're missing that's proving problematic
    – Tom J Nowell
    Nov 15, 2011 at 23:20
  • Thanks i've updated the question with the type of thing i'm trying to get. The image isn't getting displayed though, have i missed something?
    – Corbula
    Nov 16, 2011 at 14:34
0

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>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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