Support

Account

Home Forums General Issues List Custom Taxonomy with Custom Fields

Solved

List Custom Taxonomy with Custom Fields

  • Hi there.
    Im looking for way to add image size ( like: ‘thumbnail’ or ‘large’) to this loop:

    <div class="rk-row">
    <?php 
    $counter=1;
    $terms = get_terms( 'region' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<div">';
         foreach ( $terms as $term ) {
    		 if ($term->parent > 0) { continue; }
    	   $term_link = get_term_link( $term );
    	   echo '<div class="col-md-6 destination-loop">';
    	   echo '<img src="' . get_field('region_image', $term->taxonomy . '_' . $term->term_id) . '"/>';
           echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></h3>';
    	   echo '<p>' . $term->description . '</p>';
    	   echo '</div>'; 
    	   
    		   if ($counter  == 2) {
      echo '<div class="clearfix"></div></div><div class="rk-row">'; 
      $counter=0;
    
    		} 
    			
         $counter++;        
         
    	 
    	 }
    	 
    	 
     }
     
     ?>
     <div class="clearfix">
    </div></div>

    I assume I need to switch to return image object in my image custom field,
    but then what ?
    Can some one give me a hint ?
    Thanks 😉

  • You can either change to an image object or ID for return values. My preference is ID but it depends on if I need more than one image size.

    If i’m only going to need one size I use ID and then wp_get_attachment_image_src() http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src.

    If I’m going to use multiple sizes the I have ACF return the image object.

    For using the image object you would use something like this

    
    $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    $src = $image['sizes'][$size];
    echo '<img src="' . $src .'"/>';
    

    You can see the entire image object to help you work on it by doing this:

    
    $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    echo '<pre>'; print_r($image); echo '</pre>';
    
  • Thi is how I solved it:

    <div class="dest-box">
    <div class="rk-row">
    <?php 
    $counter=1;
    $terms = get_terms( 'region' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<div">';
         foreach ( $terms as $term ) {
    		 if ($term->parent > 0) { continue; }
    	   $term_link = get_term_link( $term );
    	   $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    	   $size = 'homepage-thumb';
    	   $thumb = $image['sizes'][ $size ];
    	$width = $image['sizes'][ $size . '-width' ];
    	$height = $image['sizes'][ $size . '-height' ];
    	   echo '<div class="col-md-6">';
    	   echo '<div class="destination-loop">';
    	   echo '<img src="' . $thumb . '"/>';
           echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></h3>';
    	   echo '<p>' . $term->description . '</p>';
    	   echo '</div></div>'; 
    	   
    	   
    		   if ($counter  == 2) {
      echo '<div class="clearfix"></div></div><div class="rk-row">'; 
      $counter=0;
    
    		} 
    			
         $counter++;        
         
    	 
    	 }
    	 
    	 
     }
     
     ?>
     <div class="clearfix">
    </div></div>
    
    </div>

    Thanks very much mate !

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘List Custom Taxonomy with Custom Fields’ is closed to new replies.