Skip to main content
modified per op's new info
Source Link
bynicolas
  • 2.3k
  • 1
  • 10
  • 19
// 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).'">'.get_the_post_thumbnail()$img_markup.'</a></div>';

    endwhile;
  endif;
  wp_reset_postdata();
  return $output;
}
add_shortcode( 'banner', 'show_banner' );
// 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'
      )
    )
  ));


  // 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' );
// 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' );
Source Link
bynicolas
  • 2.3k
  • 1
  • 10
  • 19

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


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