Skip to main content
added 1078 characters in body
Source Link
Abdul Awal Uzzal
  • 1.3k
  • 2
  • 12
  • 20

Do you need to query only the items that has banner_link value set to https://www.mypage.com ?

If no, you can remove the meta_query part from your query.

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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() ) { ?>
        <?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

          <div class="container" align="center"><a href="<?php echo get_post_meta( $post->ID, 'banner_link', true ); ?>"><?php the_post_thumbnail(); ?></a></div>

        <?php endwhile; ?>
<?php }
  wp_reset_postdata();
}
?>

UPDATE

As you're using it inside a plugin. You can do like the following:

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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( $post->ID, 'banner_link', true ).'">'.get_the_post_thumbnail().'</a></div>';

endwhile;
endif;
wp_reset_postdata();
return $output;
}
?>

And then call it anywhere with <?php echo wptutsplus_home_page_banner(); ?>

Let me know if it works.

Do you need to query only the items that has banner_link value set to https://www.mypage.com ?

If no, you can remove the meta_query part from your query.

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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() ) { ?>
        <?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

          <div class="container" align="center"><a href="<?php echo get_post_meta( $post->ID, 'banner_link', true ); ?>"><?php the_post_thumbnail(); ?></a></div>

        <?php endwhile; ?>
<?php }
  wp_reset_postdata();
}
?>

Do you need to query only the items that has banner_link value set to https://www.mypage.com ?

If no, you can remove the meta_query part from your query.

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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() ) { ?>
        <?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

          <div class="container" align="center"><a href="<?php echo get_post_meta( $post->ID, 'banner_link', true ); ?>"><?php the_post_thumbnail(); ?></a></div>

        <?php endwhile; ?>
<?php }
  wp_reset_postdata();
}
?>

UPDATE

As you're using it inside a plugin. You can do like the following:

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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( $post->ID, 'banner_link', true ).'">'.get_the_post_thumbnail().'</a></div>';

endwhile;
endif;
wp_reset_postdata();
return $output;
}
?>

And then call it anywhere with <?php echo wptutsplus_home_page_banner(); ?>

Let me know if it works.

Source Link
Abdul Awal Uzzal
  • 1.3k
  • 2
  • 12
  • 20

Do you need to query only the items that has banner_link value set to https://www.mypage.com ?

If no, you can remove the meta_query part from your query.

<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
    'post_type' => 'banners',
    'meta_query' => array(
      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() ) { ?>
        <?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

          <div class="container" align="center"><a href="<?php echo get_post_meta( $post->ID, 'banner_link', true ); ?>"><?php the_post_thumbnail(); ?></a></div>

        <?php endwhile; ?>
<?php }
  wp_reset_postdata();
}
?>