Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom fields in post-meta.php WordPress

  • SOLVED

Hello,

I'm trying to show a custom field called "Article Source" in the post meta underneath the title of each post.

I've used the following code with the font awesome icon "globe":

<span class="article-source"><i class="fa fa-globe"></i><?php echo get_post_meta($post->ID, 'Article Source', true); ?></span>

This works. But, if "Article Source" has no value, the globe icon still appears. What code can I use so that nothing appears if there is no value for "Article Source".

Attached is an example of when article source is empty.

Thanks for any help :)


Answers (2)

2015-07-03

Hariprasad Vijayan answers:

Hi,

Change it like this
<span class="article-source"><?php if(get_post_meta($post->ID, 'Article Source', true)){?><i class="fa fa-globe"></i><?php } ?><?php echo get_post_meta($post->ID, 'Article Source', true); ?></span>


neliti comments:

Ok but what if I want to have the custom fields "Article Source", "Month" and "Year" all in succession with the globe icon, and if none of them are present then have the globe icon disappear? Like this picture: