0

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

2 Answers 2

2

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you can do it by using the "acf/format_value" hook.

add_filter('acf/format_value', 'reFormatValue', 20, 3);
function reFormatValue($value, $post_id, $field) {
  $data = [
    // list of field keys to format
    'field_XXXXXXX' => 'XXX:',
    'im_genre' => 'Genre:',
    // etc...
  ];
  $keys = array_keys($data);
  if (in_array($field['key'], $keys)) {
    $value = $keys[$field['key']] . $value;
  }
  return $value;
}

add this to your functions.php (active theme folder) or any active plugins

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.