fbpx
How to Create Conditional "IF" Statements for WordPress Custom Meta Fields

How to Create Conditional “IF” Statements for WordPress Custom Meta Fields

Sometimes you would want to output content based on WordPress meta fields – and use conditions (statements) based on whether or not the meta fields have data, etc. In this example, we are using a meta field with the name: “youtube-video-id” and value: “abcd1234”.

Instructions
Add this code to your single.php file (or similar).

<?php if (get_post_meta($post->ID, 'youtube-video-id', true)) : ?>
     <?php echo get_post_meta($post->ID, 'youtube-video-id', true) ?>
<?php endif; ?>

WordPress Hosting

Leave a Reply

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

WordPress Snippet Info

Tested with Wordpress v. 4.8
Last Reviewed: 24.09.2017

How to Create Conditional “IF” Statements for WordPress Custom Meta Fields
4.8 (96%) 5 vote[s]
Please report broken snippets.
Report Broken Snippet
WordPress Hosting - WP Engine

Recent Comments