I am trying to get a url to a custom field to display only if it is not empty. could someone please explain what i am doing wrong so that i may learn from this, i am new to php and Im really struggling to figure this out.

example 1 displays the link whether field is empty or not however when it is empty, it links to the blog post instead of the link picker field, when it is not empty the link points to the correct field.

<div class="my_link">

<?php if($mylink = get_field('article_link') ): ?>

<div class="your_custom_class">

<a href="<?php echo ($mylink[url]); ?>"target="">Link to Article</a></div> 

<?php endif; ?> </div>

example 2 only shows if the field is not empty and does not show when the field is empty but it is not a url link to the custom field and just echos the correct url to the link.

<?php if($mylink = get_field('article_link') ): ?>

<?php echo($mylink[url]); ?>

<?php endif; ?>

Thankyou very much for your time

1 Answer 1


You're testing if assigning a value to $mylink was successful in the if statement rather than testing if it's empty.

Try something like:

    $article_link = get_field('article_link');
    if( $article_link['url'] ): ?>
    <a href="<?php echo $article_link['url']; ?>"><?php echo $article_link['title']; ?></a>
<?php endif; ?> 

See Example 1 in the ACF docs.

  • Thankyou for your help, I have attempted this method, yet the link still shows and i can not understand why , and what is even more beyond me is, if the field is empty the url links to the current blog post, and if i add a link to the link picker field it links to that link, what relation does this custom field have to current post url?
    – Samwyzz
    Nov 17, 2016 at 10:58
  • Try explicitly checking if the field is not empty with something like if (!empty(get_field('article_link'))). Linking to the current page is default browser behaviour for empty links. Nov 17, 2016 at 11:07
  • I tried earlier with if isset and empty and even when there was no link attatched it was saying it is not empty
    – Samwyzz
    Nov 17, 2016 at 11:15
  • I am new to web design and was hoping if you could suggest any ideas to the cause of this, honestly at wits end at the moment, been at it all day and still no luck.
    – Samwyzz
    Nov 17, 2016 at 11:30
  • Try doing a var_dump on get_field('article_link') (put it on your page template) and see what is returned for both "empty" and "non-empty" links. Nov 17, 2016 at 13:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.