0

It might be a stupid question, but I can't get it to work..

Problem
I have no idea how to link my image on the frontend with the url from the database.

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. Also on each post edit page, there is a input area for the banner_url. There I give my banner the link where it should go after clicking on it on the frontend. I'm able now to display the image I want on the page I want. But now I need to get the url out off the database and connent it to my image.

Code

<?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 the_post_thumbnail(); ?></a></div>

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

Question
My question is: How do I get the banner_url out of the database? And how am I able to get it to work with the image?

Thanks in advance!

EDIT PROBLEM SOLVED
Thanks to Abdul Awal

<?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( get_the_ID(), 'banner_link', true ).'">'.get_the_post_thumbnail().'</a></div>';

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

2 Answers 2

0

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.

9
  • Yes, because I've got more banners but with another meta_value.
    – W. White
    Commented Mar 21, 2017 at 14:16
  • Ok. It should work for you then. Have you tried the code ? Commented Mar 21, 2017 at 14:18
  • No, it doesn't work. It only refresh the page. Maybe it's because the post_id is different because the function is in my pluginfolder. And where I call the function and want to display the image, is in my theme folder?
    – W. White
    Commented Mar 21, 2017 at 14:22
  • Edited answer. Please try with the newly added code. Commented Mar 21, 2017 at 14:29
  • Image is displayed also, but the link doesn't work either. I have no clue at all what is wrong :(
    – W. White
    Commented Mar 21, 2017 at 14:36
1

You are looking for get_post_meta()

Your loop should look like:

<?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>

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

<?php endwhile; ?>

https://developer.wordpress.org/reference/functions/get_post_meta/

4
  • Thanks for your time! Will be appreciated but it doesn't work. This function is in my plugin file, and on another .php file in my theme folder I call the function. So maybe the get_the_ID() is not correct? And the banner_link is the meta_key but I need the meta_value from the banner_link.
    – W. White
    Commented Mar 21, 2017 at 14:11
  • Since you're already in a loop try just $post->ID instead of get_the_ID(). echo get_post_meta should spit out the meta_value you need.
    – WebElaine
    Commented Mar 21, 2017 at 14:16
  • Hi Elaine, I've already tried it, but it won't work. It will only refresh the page..
    – W. White
    Commented Mar 21, 2017 at 14:23
  • Are you sure the meta field have this name banner_link ? You may try do a var_dump(get_post_custom(get_the_ID()); to see all the meta fields saved in the post Commented Mar 21, 2017 at 18:26

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.