I have some fields (WordPress) and sometimes one of them is empty. I want to display the content of ‘the_excerpt’ when the ‘short_description’ is not filled in.
here is what I came with:
if (empty(the_field('short_description'))) { the_excerpt(); } else { the_field('short_description'); }
Unfortunately it displays both short_description and the except after that. What is wrong here? Do I miss something? For me the code looks good.
Advertisement
Answer
To Check if value exists first use get_field()
function instead of the_field()
Please have a look on example which shows how to check if a value exists before displaying it.
<?php if( get_field('short_description') ): ?> <?php the_field('short_description'); ?> <?php else: ?> <?php the_excerpt(); ?> <?php endif; ?>
Or you can user in another way like :
$isValue = get_field( "short_description" ); if( $isValue ) { echo $isValue ; } else { the_excerpt(); }