M
mw108
New Member
Hello,
I want to display an image via a custom shortcode:
But it isn't working. There is simply nothing displayed. Neither in the editor, nor in the front end. Also not if I remove the brackets [ ... ] around the shortcode and try "brand_banner" only.
However, if I use a simple text editor widget and put the shortcode in, it works as expected.
This is the code for the shortcode:
I thought maybe it is because it doesn't expect rendered image HTML, but only an image URL. So I changed the code to this, but it doesn't work either:
So, what am I doing wrong here? How can I display an image via Dynamic Shortcode?
Thank you!
I want to display an image via a custom shortcode:
But it isn't working. There is simply nothing displayed. Neither in the editor, nor in the front end. Also not if I remove the brackets [ ... ] around the shortcode and try "brand_banner" only.
However, if I use a simple text editor widget and put the shortcode in, it works as expected.
This is the code for the shortcode:
PHP:
public function shortcode_brand_banner( $atts ){$brand = $this->getRandomBrandFromPool();
$variantIndex = 1;
$variantLabel = [ '0', 'a', 'b', 'c' ];
// Return the rendered image HTML.
return ( types_render_field( 'image-brand-' . $variantLabel[ $variantIndex ], [ 'post_id' => $brand->post->ID ] ) );
} I thought maybe it is because it doesn't expect rendered image HTML, but only an image URL. So I changed the code to this, but it doesn't work either:
PHP:
public function shortcode_brand_banner( $atts ){$brand = $this->getRandomBrandFromPool();
$variantIndex = 1;
$variantLabel = [ '0', 'a', 'b', 'c' ];
// Only return image URL.
$image = get_post_meta( $brand->post->ID, 'wpcf-image-brand-' . $variantLabel[ $variantIndex ] )[ 0 ];
return ( $image );
} So, what am I doing wrong here? How can I display an image via Dynamic Shortcode?
Thank you!
Elementor Services