Home Forums ACF PRO Perform action by value of field


Perform action by value of field

  • I have a field “myfield” that can have 5 different values. I would now like to display a specific icon for each of those values. So I would need a query that says:

    – get value of “myfield”
    – if value is “value1” -> show icon1
    – if value is “value2” -> show icon2
    – etc.

    This can’t be too complicated, but I’m unable to figure that out. Any help would be appreciated. Thank you!

  • What type of field is it?

  • Oh, sorry. It’s field type “taxonomy”

  • So how are you going to decide what icon to show?

    I personally would add a field to the taxonomy so that you can specify the icon when adding or editing the term and then I’d do something like.

    $terms = get_field('taxonomy_field');
    if ($terms) {
      foreach ($terms as $term) {
        echo get_field('icon_field', $term);

    There would obviously need to be more code, but that’s the basics.

  • Thank you, but that’s not suitable. I’m doing a relaunch of a site that has already 300+ posts and in that case I would have to reopen them all. That’s what I would like to avoid. The taxonomy for instance is used for filtering purposes only. I hope I could use it for that purpose as well (I have another case like that in my website and finding the solution, I could get rid of 4-5 fields).

    Also I made an error since it’s not an Icon in the meaning of “Icon font” but a graphic which has a distinct URL. So the logic would be:

    If value is 1 then load div with url-to-graphic 1
    If value is 2 then load div with url-to-graphic 2

    Ah, the values are in plain text.

  • If you have a lot of terms I would use a switch, and it depends on if your field is returning 1 term or multiple temrs. Assuming that the field is returning multiple terms.

    $terms = get_field('your-tax-field');
    if ($terms) {
      foreach ($terms as $term) {
        switch ($term->term_id) { // could also be any term value
          // cases would depend on above
          case 1:
            // output for case 1
          case 2:
            // output for case 2
          case 3:
            // output for case 3
          // etc...
        } // end switch
      } // end foreach term
    } // end if terms
  • It’s only returning one specific term. I’ll try that out. Thank you very much!

    However, in that minute I found a solution that was too simple … I named my graphics exactly like the term names and queried the term name to complete the URL. Like so:

    /img/tour-badges/<?php $term = get_field(‘tourcharakter’); if( $term ): ?><?php echo $term->name; ?><?php endif; ?>.png”>

    The result is URL/term1.png // URL/term2.png etc. Sometimes you don’t manage to see the forest for all the trees that are in the way (like we say in Germany 😉

    Thanks again!

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

The topic ‘Perform action by value of field’ is closed to new replies.