Question

I am using the Custom Metaboxes and Fields for WordPress add-on to create custom fields for custom post types. However, I need a way to display content only if a value exists for a specific custom field.

Currently, I am using this code:

<?php 
$url = get_post_meta($post->ID, 'snippet-reference-URL', true); 
    if ($url) {
    echo "<p><a href='$url'>Reference URL</a></p>";
} ?>

However, this displays content if the field is present (which in this case, is always). I need code that will only display content if a specific field has a value.

Was it helpful?

Solution

check this example given on codex page

<?php 
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
} 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top