-
I’m using a content layout Element for a specific post type. In that template, I’m showing several images that are set on each post — one of them is the featured image, the 2 others are saved in meta fields (using MetaBox.io).
When I pull the images with dynamic tags, I always get the full size image. With the featured image, I know I have an option for size, but for images from meta fields I don’t see how I can change the size. I know no solution is documented, but is there a way to pass a size option to a dynamic tag pulling an image from a meta field?
My dynamic tag is:
{{post_meta key:org_photo.url}}for context.Thank you
-
Hi there,
Yes, you are right, there’s no sizing option for the post meta images, unfortunately.
It simply outputs the URL of the image. -
Thank you Ying. Is there any custom code that could add that functionality? Or is it a matter of creating custom tags? There must be a way. For performance it’s just not acceptable to display the full size image everywhere…
-
Try this PHP code to generate a new dynamic tag post meta image, so you can use a dynamic tag like this
{{post_meta_image key:org_photo|size:medium}}.add_action( 'init', function() { new GenerateBlocks_Register_Dynamic_Tag( [ 'title' => __( 'Post Meta Image', 'generateblocks' ), 'tag' => 'post_meta_image', 'type' => 'post', 'supports' => [ 'meta', 'source', 'image-size' ], 'description' => __( 'Access post meta image by key for the specified post. Returns the specified image size URL.', 'generateblocks' ), 'return' => 'yh_get_post_meta_image', ] ); }); function yh_get_post_meta_image( $attributes ) { // Get meta key and size $meta_key = ! empty( $attributes['key'] ) ? $attributes['key'] : ''; $size = ! empty( $attributes['size'] ) ? $attributes['size'] : 'full'; $post_id = ! empty( $attributes['source'] ) ? $attributes['source'] : get_the_ID(); if ( ! $meta_key || ! $post_id ) { return ''; } $image_field = get_post_meta( $post_id, $meta_key, true ); if ( ! $image_field ) { return ''; } // Handle all ACF return types if ( is_array( $image_field ) && isset( $image_field['ID'] ) ) { $image_id = $image_field['ID']; } elseif ( is_numeric( $image_field ) ) { $image_id = $image_field; } elseif ( is_string( $image_field ) && filter_var( $image_field, FILTER_VALIDATE_URL ) ) { return esc_url( $image_field ); // already a URL } else { return ''; } $image_src = wp_get_attachment_image_src( $image_id, $size ); return $image_src ? esc_url( $image_src[0] ) : ''; }
- You must be logged in to reply to this topic.