CrawlerWebSolutions
CrawlerWebSolutions

Reputation: 35

How can I display an image if the meta_key=x AND meta_value=y in WordPress?

In my WordPress MySQL DB there is a meta_key in wp_postmeta table with the value mazda-certified. Additionally there is a meta_value associated with the meta_key that reads: "Yes" or "No".

I need to figure out how to display an image if and only if the meta_key=mazda-certified and the meta_key=yes.

Any help with this is tremendously appreciated.

I have been researching this for hours and unfortunately only pulling up information regarding UserMeta.

Upvotes: 0

Views: 163

Answers (1)

Benoti
Benoti

Reputation: 2210

As I don't really know how want to display you image, my easiest way to achieve this, is to create a shortcode that you'll be able to place anywhere you want

In your functions.php file :

   add_shortcode('certified-image', 'add_certified_image');

   function add_certified_image($atts, $content = ""){
      global $post;

      if(get_post_meta($post->ID,'mazda-certified', true) == 'yes'){
          $content = '<img src="image-yes.jpg"/>';
      }
      else{
          $content = '<img src="image-no.jpg"/>';
      }
      return $content;
   }

In your template :

   echo do_shortcode('[certified-image]'); 

It's a short example, read more about add_shortcode on the codex to add attributes to the shortcode.

Upvotes: 1

Related Questions