0

I'm really unexperienced when it comes to conditonal statements with php and unfortunately the documentation of Advanced Custom Fields isn't covering this topic as fair as I know. But anyway, what I'm trying to do is to display some markup and custom fields but only if one of the fields or both fields have data.

For example I have a footer that could display an email address and/or website link. If both custom fields are not filled with data the footer markup shouldn't be displayed. If there's an email address and/or website link available it should display the footer markup and of course the data of the fields.

This is what I currently have:

<footer>

<?php if( get_field('website') ): ?>

<a href="<?php the_field('website'); ?>"><?php the_field('website'); ?></a>

<?php endif; ?>     

<?php if( get_field('emailaddress') ): ?>

<a href="mailto:<?php the_field('emailaddress'); ?>"><?php the_field('emailaddress'); ?</a>

<?php endif; ?>                                 

</footer>

But this code displays the footer markup even if both custom fields are not available. How can I make it that the footer markup is only shown if one of both of the custom fields have a value?

If been trying to do it with an || statement but that returns an unexpected T_BOOLEAN_OR error

<?php if( get_field('website') ) || ( get_field('emailaddress') ): ?>

// do this

<?php endif; >

If anyone knows if this is possible, how to do this or could point me in the right direction that would be much appreciated.

closed as off-topic by Johannes Pille, birgire, TheDeadMedic, Pieter Goosen, Nicolai Sep 5 '14 at 11:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Johannes Pille, birgire, TheDeadMedic, Pieter Goosen, Nicolai
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You should properly indent your code. It just makes it easier to read and to debug. As it stands, it makes it hard to read – Pieter Goosen Aug 14 '14 at 17:01
2

I believe you are on the right track with your second idea there, but you have some syntax errors with your conditional. In PHP all of your logic should be wrapped inside one set of ( ) for your conditional. Try this:

<?php
if ( get_field('website') || get_field('emailaddress') ) {
    ?>
    <footer>
        <?php
        if ( get_field('website') ) {
            ?>
            <a href="<?php the_field('website'); ?>"><?php the_field('website'); ?></a>
            <?php
        }
        if ( get_field('emailaddress') ) {
            ?>
            <a href="mailto:<?php the_field( 'emailaddress' ); ?>"><?php the_field( 'emailaddress' ); ?></a>
            <?php
        }
        ?>
    </footer>
    <?php
}
  • Hi Josh, thanks that was the problem indeed, thanks for the clarification. Your example has two minor typos in the code but they were easy to fix. – NielsPilon Aug 14 '14 at 14:37
  • @NielsPilon where are they? I'd like to fix them in the answer for the future. – Josh Mountain Aug 14 '14 at 14:56
  • <?php the_field('emailaddress'); ? missing a '>' at the end and the final endif; is missing a '?' – NielsPilon Aug 14 '14 at 14:57
  • for reference: if ( <a statement that evaluates to true> ) { do this } else { do that } – Tom J Nowell Aug 14 '14 at 15:13
-2

I prefer to keep my PHP inline when possible to avoid scattering its start or end tags and possibly miss one. It's closer to the OP's original attempt and imo reads cleaner/clearer:

<?php if(get_field('website') || get_field('emailaddress')) : ?>
  <footer>
    <?php if(get_field('website')) : ?>
      <a href="<?php the_field('website'); ?>"><?php the_field('website'); ?></a>
    <?php endif; ?>
    <?php if(get_field('emailaddress')) : ?>
      <a href="mailto:<?php the_field('emailaddress'); ?>"><?php the_field('emailaddress'); ?></a>
    <?php endif; ?>
  </footer>
<?php endif; ?>
  • So you downvote my answer because of your opinion on how code should be formatted? – Josh Mountain Aug 14 '14 at 16:01
  • 2
    @JoshMountain style of formatting is much easier to read. Also, it is more code editor friendly, easier to debug, and much cleaner. And BTW, you are abusing php tags. Only use php tags when switching from php to html and vica-versa. I would rather use his mark up than yours – Pieter Goosen Aug 14 '14 at 16:41

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