Home Forums General Issues Should I use conditional logic to display fields?


Should I use conditional logic to display fields?

  • A very basic question that I can’t seem to find an answer to and really hope someone can advise. Sorry, it’s so simple – I can (just about) adapt php code but am not a coder.

    When I want to display the value of a WYSIWYG field, should I use the simple

    <?php the_field(‘field_name’); ?>

    Or should I put it in conditional logic in case the field value is null:

    <?php if( get_post_meta($post->ID, “field-name”, true) ): ?>
    <?php echo get_post_meta($post->ID, “field-name”, $single = true);?>
    <?php endif; ?>

    I don’t want to do anything else with these particular fields, just display them.

    I’m asking both with regards to page load speed (eg number of database queries) and also to try to prevent some of the 504 crashes my site has been having recently when I am editing pages. (The crashes began when my husband rebuilt our AWS server in order to upgrade php for the upcoming Woocommerce php change requirement; we found that changing from php 7.4 to 8.1 helped matters but has not fixed issues and so I am now looking at trying to revise/check all of my coding.)

  • If you use the_field() on a field with no value then is simply will not output anything at all. There’s no need to check the field unless there is something else that is dependent on the value, for example a container element.

  • Many thanks John!! I had been unsure if it would send the code into a loop if there were no value there.

    Thanks so much for the answer!!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.