0

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.
4
  • is the code //start by setting the query is your show_banner function?
    – bynicolas
    Commented Mar 22, 2017 at 13:13
  • Oops my bad, did edit my code.
    – W. White
    Commented Mar 22, 2017 at 13:16
  • ok, you can pass an $att argument to that function containing additional custom value to that shortcode... I'll write an example up if that is what you want
    – bynicolas
    Commented Mar 22, 2017 at 13:20
  • Yeah, what I want is when I need the image with thumnail_id=573 to be displayed, just echo: <?php echo do_shortcode("[banner _thumbnail_id='573']");?> for example
    – W. White
    Commented Mar 22, 2017 at 13:30

1 Answer 1

0
// 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' );

  // 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'
      )
    )
  ));


  $img_markup = ! empty( $att[ 'thumbnail_id' ] ) ? sprintf( '<img src="%s" />', wp_get_attachment_image_src( $att[ 'thumbnail_id' ] ) ) : '';


  // 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).'">'.$img_markup.'</a></div>';

    endwhile;
  endif;
  wp_reset_postdata();
  return $output;
}
add_shortcode( 'banner', 'show_banner' );
13
  • First thanks your your time. But it doesn't work. Right now my image is selected by the meta_query. So if I change the value to some other url, I will get another image. The point right now is that the code must change so, that when I say I want the image with id: 123 in the shortcode, the image 123 is displayed. With mine and your edited code, I will always get the image linked to mypage.com. I hope your understand what I need to achieve. Appreciate your suggetion!
    – W. White
    Commented Mar 22, 2017 at 13:50
  • well it's because I didn't finish the code to achieve that. I just added what was needed to grab the image id from the shortcode... so if I get this correctly, if you pass an image id to the shortcode, you want that image to be displayed, if not, you want the image from the query to be displayed?
    – bynicolas
    Commented Mar 22, 2017 at 14:04
  • Yes, exactly. But if not, I don't want any image to display. An image only need to displayed when I pass an image id to the shortcode.
    – W. White
    Commented Mar 22, 2017 at 14:12
  • ok let me edit that real quick
    – bynicolas
    Commented Mar 22, 2017 at 14:17
  • done, but I think it still misses something... if you don't have an image id, what are you supposed to show, just the link to the banner? because now if there is no image id, we are missing some link anchor!
    – bynicolas
    Commented Mar 22, 2017 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.