Situation
I've created a Custom Post Type called 'Banners'. On each post I'm able to add a featured image. This will be saved in my database and will get an _thumbnail_id. Also on each post edit page, there is a custom meta box for the banner_url and the target attribute. There I give my banner the link where it should go after clicking on it on the frontend. And the target attribute will open the image in the correct way. This will be correctly send to my wpdb.
Plugin code:
<?php
// function to show a banner using query of banner post type
function show_banner() {
// start by setting up the query
$get_banner = new WP_Query( array(
'post_type' => 'banners',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'banner_link',
'value' => 'https://www.mypage.com'
)
)
));
// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) : while ( $get_banner->have_posts() ) : $get_banner->the_post();
$output = '<div class="container" align="center"><a href="'.get_post_meta( get_the_ID(), 'banner_link', true ).'"
target="'.get_post_meta( get_the_ID(), 'target', true).'">'.get_the_post_thumbnail().'</a></div>';
endwhile;
endif;
wp_reset_postdata();
return $output;
}
add_shortcode( 'banner', 'show_banner' );
//END
?>
Page.php
<?php echo do_shortcode("[banner]");?>
Question
I use a meta_query to select the url. It works perfectly and displays the correct image. Also the link and target works fine. But what I want is that
I can add a thumbnail_id to the do_shortcode echo and then the correct image is displayed on the frontend with the right url and target around it.
I hope someone can help me in the right direction. Thanks in advance.
EDIT SOLUTION
Managed to fix the problem
Special thanks to bynicolas for getting me in the right direction!
Plugin code:
<?php
// function to show a banner using query of banner post type
function show_banner( $att ) {
// Set default $att values if none is provided
// $att is an array so access the shortcode value via $att['thumbnail_id']
// in the [banner thumbnail_id="12"] shortcode
$atts = shortcode_atts(
array(
'_thumbnail_id' => '',
), $atts, 'banner' );
//print_r('Thumbnail_id: ' . $att['_thumbnail_id'] . '<br>');
//start by setting up the query
$get_banner = new WP_Query( array(
'post_type' => 'banners',
)
);
//now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) :
while ( $get_banner->have_posts() ) : $get_banner->the_post();
$thumbnail_id = get_post_meta(get_the_ID(), '_thumbnail_id', true);
$image_url = get_post_meta(get_the_ID(), 'banner_link', true);
$target = get_post_meta(get_the_ID(), 'target', true);
if ($thumbnail_id === $att['_thumbnail_id']) {
// print_r($thumbnail_id . ' ');
// print_r($image_url . ' ');
// print_r($target . '<br>');
$output = '<div class="container" align="center"><a href="'.get_post_meta( get_the_ID(), 'banner_link', true ).'"
target="'.get_post_meta( get_the_ID(), 'target', true).'">'.get_the_post_thumbnail().
'</a></div>';
}
endwhile;
endif;
wp_reset_postdata();
return $output;
}
add_shortcode( 'banner', 'show_banner' );
?>
Page.php
<?php echo do_shortcode('[banner _thumbnail_id="584"]');?>
//584 is the thumbnail_id, so change that to your own thumbnail_id.
//start by setting the query
is yourshow_banner
function?$att
argument to that function containing additional custom value to that shortcode... I'll write an example up if that is what you want