WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy Images
Help displaying taxonomy images within the loop (4 posts)

  1. garydavison
    Member
    Posted 2 years ago #

    Hi,
    First thanks for the great plugin, I'm trying to display images for custom taxonomies for each post, but I'm struggling. I have the following code:

    <?php $tax = 'colours'; ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    					<div class="panel">
    	                	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(212, 212)); ?></a>
    	              		<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	                    <div class="swatches">
    	                   	  <ul>         
    
    	                      	<?php $terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy'=> $tax ));
    							   foreach( (array) $terms as $term ) {
    							   	   echo '<li>' . wp_get_attachment_image( $term->image_id, 'thumb' ) . '</li>' ;
    							   }
    					         ?>
    	                      </ul>
    
    	                    </div>
    	                </div>
    				<?php endwhile; wp_reset_query(); ?>

    It does display the images for the custom taxonomies, unfortunately it displays them in every post, not just the posts that have bee allocated the taxonomy. What do I need to change to ensure that the taxonomy images are displayed for the post that has been allocated to rather than for all of the posts.

    Thanks
    Gary

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

  2. Michael Fields
    Themer
    Plugin Author

    Posted 2 years ago #

    Hi,

    It looks like you might want to use one of the the filters. There are two:

    taxonomy-images-get-the-terms

    and

    taxonomy-images-list-the-terms

    Maybe try something like this:

    print apply_filters( 'taxonomy-images-list-the-terms', '', array(
    	'after'        => '</div>',
    	'after_image'  => '</span>',
    	'before'       => '<div class="my-custom-class-name">',
    	'before_image' => '<span>',
    	'image_size'   => 'detail',
    	'post_id'      => 1234,
    	'taxonomy'     => 'post_tag',
    ) );
  3. garydavison
    Member
    Posted 2 years ago #

    Hi thanks for the response, I have tried the following:

    <?php query_posts( 'post_type=bedrooms&orderby=name&order=ASC' ); ?>
    				<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    					<div class="panel">
    	                	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(212, 212)); ?></a>
    	              		<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	                    <div class="swatches">
    	                   	  <ul>
    					       <?php
    					         print apply_filters( 'taxonomy-images-list-the-terms', '', array(
    								'after'        => '</li>',
    								'after_image'  => '</span>',
    								'before'       => '<li>',
    								'before_image' => '<span>',
    								'image_size'   => 'detail',
    								'post_id'      => 1234,
    								'taxonomy'     => 'colours',
    							) );
    							?>
    	                      </ul>
    	                    </div>
    	                </div>
    				<?php endwhile; wp_reset_query(); ?>

    And it doesn't appear to work, is there something obvious that I'm missing?

    Thanks
    Gary

  4. easilyamused
    Member
    Posted 2 years ago #

    @garydavison You will never learn if you just copy and past.

    Did you look at what the code @Michael Fields did?
    http://wordpress.org/extend/plugins/taxonomy-images/ Tells you what everything does.

    Try customizing your array for your needs and it should work just fine. It worked for me.

    I pasted my customized code below. It show the 3 newest blog post and includes there category image.

    - tough love

    <h2>Latest News</h2>
    	<ul>
    		<?php
    		$recent_posts = new WP_Query(array(
    			'post_type' => $post_type_array,
    			'cat' => $categories,
    			'posts_per_page' => 3
    		));
    		?>
    		<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
    			<li>
    				<?php
    				print apply_filters( 'taxonomy-images-list-the-terms', '', array(
    					'taxonomy'     => 'category',
    				) ); ?>
    				<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo get_template_directory_uri(); ?>/img/service-sm-.png" alt=""/></a>
    			<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
    			<span class="block-meta"><?php the_time(get_option('date_format')); ?>, <?php comments_popup_link(); ?></span>
    		</li>
    		<?php endwhile; ?>
    	</ul>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic