Skip to content
Advertisement

add specific text to a customm fields with php (wordpress)

I want to add text “Genre:” to the beginning of this custom field

<?php the_field('im_genre'); ?>

the reason that i dont simply add something like this

 <li>Genre:<?php the_field('im_genre'); ?> </li>
      

is that i dont want to this text be shown when there isnt this field for a post

Advertisement

Answer

You can check if the field exists and then display your HTML element.

<?php if( get_field('im_genre') ): ?>
    <li>Genre:<?php the_field('im_genre'); ?> </li>
<?php endif; ?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement