Support

Account

Home Forums General Issues Display featured images of selected taxonomy terms

Solved

Display featured images of selected taxonomy terms

  • I created:

    • a custom post type (courses)
    • a custom taxonomy (accreditations)

    I added:

    • a repeater custom field (accreditations) to the course posts, containing a taxonomy subfield (organizations – select dropdown) which loads and saves the taxonomy terms.
    • a image custom field (featured_image) to the taxonomy terms

    Now I would like to show the featured images of the taxonomy terms I selected for a particular course. Tried the code below, but this code shows the featured images of all taxonomy terms, not only the ones I selected for the particular course.

    function custom_accreditation_logo () {
    	ob_start();
    		$terms = get_terms( array( 'taxonomy' => 'accreditations', 'hide_empty' => false ) );
    		foreach ( $terms as $term ) {
    			$image = get_field('featured_image', 'accreditations_' . $term->term_id );
    			echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">'; 
    		}
    	$content = ob_get_clean(); // store buffered output content.
    	return $content;
    }
    add_shortcode( 'accreditatie_logos', 'custom_accreditation_logo' );
    

    Does anyone know how to only show the featured images of the selected taxonomy terms (not the unselected ones)?

  • I came up with the following solution:

    function custom_accreditation_logo () {
    	ob_start();
    		$queried_object = get_queried_object(); 
    		$post_id = $queried_object->ID; 
    		$terms = wp_get_post_terms( $post_id, 'accreditations' );
    		//$terms = get_terms( array( 'taxonomy' => 'accreditations', 'hide_empty' => false ) );
    		echo '<div class="acc_logos">';
    		foreach ( $terms as $term ) {
    			$image = get_field('featured_image', 'accreditations_' . $term->term_id );
    			echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">'; 
    		}
    		echo '</div>';
    	$content = ob_get_clean(); // store buffered output content.
    	return $content;
    }
    
    add_shortcode( 'accreditatie_logos', 'custom_accreditation_logo' );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.