• Hello
    I try to display the term image in a function.
    Any help would be really appreciated.

    add_shortcode( 'cat_suppliers', 'display_cat_suppliers' );
    function display_cat_suppliers($term_id)
    {
    	?>
    	<div class="supplier_categories">
    	<?php
      ob_start();
        // your taxonomy name
    $tax = 'supplier_category';
    
    // get the terms of taxonomy
    $terms = get_terms( $tax,  [
    	'parent' => 0,
    	'number' => 8,
      'hide_empty' => true, // do not hide empty terms
    ]);
    
    	// Randomize Term Array
    shuffle( $terms );
    
    // loop through all terms
    foreach( $terms as $term ) {
    
    	$image = apply_filters( 'taxonomy-images-queried-term-image', '', array(
        'image_size' => 'portfolio_4_col'
    ) );
      // if no entries attached to the term
      //if( 0 == $term->count )
        // display only the term name
       // echo '<h4>' . $term->name . '</h4>';
    
      // if term has more than 0 entries
        // display link to the term archive
        ?>
        <div class="supplier_4_col_item_wrapper mix">
        <a href="<?php echo get_term_link( $term ); ?>">
        <div class="supplier-item">
    		<figure>
                            
    <div class="supplier-item-thumbnail">
    
    <?php print $image;?>
                                                        
    </div>                           
    <figcaption class="supplier-item-hover">
    
                       <div class="cat-supplier-item-title">
    				<span> <?php echo $term->name; ?></span>
                              </div>     
                                 
    </figcaption>
                            
                                
    
    </figure>
    
    </div>
    </a>
    </div>
    <?php
    
    }
    	return ob_get_clean();
    	?>
    </div>
    	<?php
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter whoaloic

    (@whoaloic)

    The following code is working.

    function display_cat_suppliers( $atts, $content = null )
    {
    	extract(shortcode_atts(array(
    		"number" => '9999'
    	), $atts));
    	?>
    	<div class="cat-suppliers">
    	<?php
      ob_start();
        // your taxonomy name
    $tax = 'supplier_category';
    
    // get the terms of taxonomy
    $terms = get_terms( $tax,  [
    	'parent' => 0,
    	'number' => $number,
      'hide_empty' => true, // hide empty terms
    ]);
    
    	// Randomize Term Array
    shuffle( $terms );
    
    // loop through all terms
    foreach( $terms as $term ) {
    // Set your defined term ID
    $tem_id = $term->term_id;; // (an integer)
    
    // Get the array of Term ID/Image ID pairs
    $taxonomy_images = get_option( 'taxonomy_image_plugin' );
    
    $attachment_id = $taxonomy_images[$tem_id]; // Image ID
    $image = wp_get_attachment_image( $attachment_id, 'portfolio_4_col' ); // Image
    $term = get_term( $tem_id ); // The WP_Term object
    $link = get_term_link( $tem_id ); // The term link
        ?>
        <div class="supplier_4_col_item_wrapper mix">
    
                        <a href="<?php echo get_term_link( $term ); ?>">
                                                                      
                                             <div class="style-item">
                           <div class="front">
    			<!-- front content -->
    			 <figure>
                            
    <div class="style-item-thumbnail">
    
    <?php echo $image; ?>
    
                              
    
    </div>                            <figcaption class="style-item-hover"></figcaption>
                           	<div class="style-item-title">
    				<span> <?php echo $term->name; ?> </span>
                              </div>
    </figure>
    </div>
    </div>
    		
                           
                           </a>
    
                        </div>
     
    <?php
    
    }
    	return ob_get_clean();
    	?>
    </div>
    	<?php
    }
    • This reply was modified 6 years, 9 months ago by whoaloic.

    Thanks whoaloic!!!! Worked for me too!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show term image in a query loop’ is closed to new replies.