fbpx
How to Display WordPress Custom Fields with Conditional Statements

How to Display WordPress Custom Fields with Conditional Statements

Sometimes you need to display post-specific content using WordPress custom fields. In this example we will show you how you can display custom field data using conditional statements in the form of a youtube video embed. If the post has a “youtube-video-id” defined, the content will be rendered.

Instructions
Insert content as shown below.

<?php global $wp_query; $postid = $wp_query->post->ID; ?>
     <?php /* if a custom field youtube video id is defined */ if( get_post_meta($postid, 'youtube-video-id', true) ): ?>
          <div class="video-item-wrapper">
               <iframe src="https://www.youtube.com/embed/<?php  echo get_post_meta($postid, 'youtube-video-id', true);?>?feature=oembed&autoplay=0" frameborder="0" allowfullscreen="">
               </iframe
          </div>
     <?php endif; ?>
<?php wp_reset_query(); ?>

WordPress Hosting

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Snippet Info

Tested with Wordpress v. NA

How to Display WordPress Custom Fields with Conditional Statements
4.7 (93.78%) 45 vote[s]
Please report broken snippets.
Report Broken Snippet

Recent Comments