Taxonomy Image on Custom Post Type

Support MB Term Meta Taxonomy Image on Custom Post TypeResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35179
    Ole PoetterOle Poetter
    Participant

    Hi,

    regarding to this topic https://support.metabox.io/topic/taxonomy-image-on-custom-post-type/ I try to display in Image field of a custom term on the post display.

    I try to make a shordcode like this:

    function custom_shortcode() {
    
    	$terms = get_the_terms( get_queried_object_id(), 'language' );
    	foreach ( $terms as $term ) {
    	    $term_image = rwmb_meta( 'term_flag', ['object_type' => 'term'], $term->term_id );
    	    if( is_array( $term_image ) ) {
    	        echo '<img src="'. $term_image['full_url'] .'">'; 
    	        echo '<pre>'; // just for debugging
    	        var_dump($term_image); // just for debugging
    	        echo '</pre>'; // just for debugging
    	    }
    	}
    
    }
    add_shortcode( 'termimage', 'custom_shortcode' );

    I get an php warning:
    Undefined array key "full_url"

    This is the output of the var_dump:

    array(1) {
      [385]=>
      array(15) {
        ["width"]=>
        int(40)
        ["height"]=>
        int(24)
        ["file"]=>
        string(15) "2022/03/de1.png"
        ["sizes"]=>
        array(0) {
        }
        ["image_meta"]=>
        array(12) {
          ["aperture"]=>
          string(1) "0"
          ["credit"]=>
          string(0) ""
          ["camera"]=>
          string(0) ""
          ["caption"]=>
          string(0) ""
          ["created_timestamp"]=>
          string(1) "0"
          ["copyright"]=>
          string(0) ""
          ["focal_length"]=>
          string(1) "0"
          ["iso"]=>
          string(1) "0"
          ["shutter_speed"]=>
          string(1) "0"
          ["title"]=>
          string(0) ""
          ["orientation"]=>
          string(1) "0"
          ["keywords"]=>
          array(0) {
          }
        }
        ["ID"]=>
        string(3) "385"
        ["name"]=>
        string(7) "de1.png"
        ["path"]=>
        string(111) "/var/www/vhosts/t36updxb.web5.alfahosting-server.de/mylistingshowcase.online/wp-content/uploads/2022/03/de1.png"
        ["url"]=>
        string(67) "https://mylistingshowcase.online/wp-content/uploads/2022/03/de1.png"
        ["full_url"]=>
        string(67) "https://mylistingshowcase.online/wp-content/uploads/2022/03/de1.png"
        ["title"]=>
        string(5) "de[1]"
        ["caption"]=>
        string(0) ""
        ["description"]=>
        string(0) ""
        ["alt"]=>
        string(0) ""
        ["srcset"]=>
        bool(false)
      }
    }

    Can you please assist and tell me where is my mistake?
    I'm a non professionell developer.

    Thank you!

    #35199
    Long NguyenLong Nguyen
    Moderator

    Hi Ole,

    That means you need to use a second for loop to iterate through $term_image array to get images

    $term_image = rwmb_meta( 'term_flag', ['object_type' => 'term'], $term->term_id );
    if( is_array( $term_image ) ) {
    	foreach ( $term_image as $image ) {
    		echo '<img src="'. $image['full_url'] .'">'; 
    	}
    }
    #35200
    Ole PoetterOle Poetter
    Participant

    Hi Long,

    I thank you so much!
    I have to learn a lot more 😉

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.