Manish Pushkar Jha

Full Stack Web Developer
March 1, 2021
2:39 pm

How to display a custom field if there is a data in custom field?

11 Views  •  May 10th, 2020  •  1 minute read

Hi Manish,

I am creating one WordPress theme from scratch, it’s for a law firm.

I created one custom field by enabling ‘custom field’ from ‘screen options’. My field name is ‘url’.

How to display my field value in UI?

How to make this field visible, only if there is a value written in backend?

I did a lot of research and unfortunately didn’t find any answer. Please help!


Answer

Hey Maddie,

You are doing a great work.

Paste the following codes in your UI, where you want to display  your desired ‘custom field’.

//use post meta value to display input field, wherever it's required

<?php 
$custom = get_post_meta($post->ID, 'url', true);
			 
if ($custom) { ?>
			
<a href="<?php echo get_post_meta($post->ID, 'url', true); ?>">custom text</a>
			 
<?php 
			 
} else { 
// do nothing; 
}
			 
?>