0
<?php echo $yearbuilt = get_post_meta ($post->ID, 'yearbuilt', true); if ( !empty($yearbuilt) ) ?>

I want the word "Built" before the custom field yearbuilt and only if yearbuilt is NOT empty. I've worked on this for hours and nothing I tried worked. Help!

1 Answer 1

1

It's pretty simple

echo ((get_post_meta ($post->ID, 'yearbuilt', true) != '') ? 'Built ' . get_post_meta ($post->ID, 'yearbuilt', true) : '');

get_post_meta will return something, even if there is no post meta set, in your case an empty string (since 3rd argument is set to true), so $yearbuilt won't return true to an empty check. Also, calling get_post_meta twice is not really inefficient, since it gets value from cache anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!!!! That worked and I see what I was doing wrong. Thanks for the explanation.
@user2568633 You're welcome. Don't forget to accept an answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.