• Resolved chrimbus

    (@chrimbus)


    I can pull an image on to a page using the id: (for 7 in this case)

    // image id is stored as term meta
    $image_id = get_term_meta( 7, 'image', true );
    	
    // image data stored in array, second argument is which image size to retrieve
    $image_data = wp_get_attachment_image_src( $image_id, 'full' );
    	
    // image url is the first item in the array (aka 0)
    $image = $image_data[0];

    But how would I write it so that the proper image would appear for each item in the “for each” loop?

    • This topic was modified 7 years, 7 months ago by chrimbus. Reason: code format
    • This topic was modified 7 years, 7 months ago by chrimbus.
Viewing 1 replies (of 1 total)
  • Thread Starter chrimbus

    (@chrimbus)

    Figured it out. In my case, I grabbed the $term and referenced $term->term_id and it worked.

    
    <?php
    
    $this_term = get_queried_object();
    $args = array(
        'parent' => $this_term->term_id,
        'orderby' => 'slug',
        'hide_empty' => false
    );
    $child_terms = get_terms( $this_term->taxonomy, $args );
    echo '<ul>';
    foreach ($child_terms as $term) {
    	
    	// image id is stored as term meta
    	$image_id = get_term_meta( $term->term_id , 'image', true );
    	
    	// image data stored in array, second argument is which image size to retrieve
    	$image_data = wp_get_attachment_image_src( $image_id, 'full' );
    	
    	// image url is the first item in the array (aka 0)
    	$image = $image_data[0];
    
    	if ( ! empty( $image ) ) {
    	    echo '<img src="' . esc_url( $image ) . '" />';
    	}
    
    		
        echo '<li><h3><a href="' . get_term_link( $term->name, $this_term->taxonomy ) . '">' . $term->name . '</h3></a></li>';  
    }
    echo '</ul>';
    
    ?>
    
Viewing 1 replies (of 1 total)
  • The topic ‘How to pull image automatically?’ is closed to new replies.