Support

Account

Forum Replies Created

  • Thanks, John, for your help. I appreciate it!

    It turns out that having HTML code in the checkbox field itself is not a good idea. When using the field, the selections don’t stick (i.e. the selected options are gone the next time you edit the post).

    So I went with only plain text values for the checkbox field and coded everything else in the template. Unfortunately, I read your sample code too late – going with “switch” would have been a good idea. As I’m not experienced with PHP, I handcrafted this subpar solution:

    $tools_choices = get_field('tools');
    
    if( $tools_choices ): ?>
      <h3 class="acf-title">Tools</h3>
    		  
      <?php foreach( $tools_choices as $name ): ?>
    
        <?php if( $name == 'Tool1' ): ?>
        <a href="https://url1.com/" target="_blank"><i class="fab fa-icon-x"></i><?php echo $name; ?></a>
        <?php elseif( $name == 'Tool2' ): ?>
        <a href="https://url2.com/" target="_blank"><i class="fab fa-icon-y"></i><?php echo $name; ?></a>
        <?php elseif( $name == 'Tool3' ): ?>
        <a href="https://url3.com/" target="_blank"><i class="fab fa-icon-z"></i><?php echo $name; ?></a>
        <?php else: ?>
        <p><i class="fab fa-icon-a"></i><?php echo $name; ?></p>
        <?php endif; ?>
    
      <?php endforeach; ?>
    
    <?php endif; ?>

    It’s not the best code, but it works. If I later have the time and energy, I might change my code to a cleaner version based on your input.

    Thanks again,
    Patrick

  • Thanks, John, for your help. I appreciate it!

    It turns out that having HTML code in the checkbox field itself is not a good idea. When using the field, the selections don’t stick (i.e. the selected options are gone the next time you edit the post).

    So I went with only plain text values for the checkbox field and coded everything else in the template. Unfortunately, I read your code sample too late. Going with “switch” would have been a good idea, but as I’m not experienced with PHP, I handcrafted this subpar solution:

    $tools_choices = get_field('tools');
    
    if( $tools_choices ): ?>
      <h3 class="acf-title">Tools</h3>
    		  
      <?php foreach( $tools_choices as $name ): ?>
    
        <?php if( $name == 'Tool1' ): ?>
        <a href="https://url1.com/" target="_blank"><i class="fab fa-icon-x"></i><?php echo $name; ?></a>
        <?php elseif( $name == 'Tool2' ): ?>
        <a href="https://url2.com/" target="_blank"><i class="fab fa-icon-y"></i><?php echo $name; ?></a>
        <?php elseif( $name == 'Tool3' ): ?>
        <a href="https://url3.com/" target="_blank"><i class="fab fa-icon-z"></i><?php echo $name; ?></a>
        <?php else: ?>
        <p><i class="fab fa-icon-a"></i><?php echo $name; ?></p>
        <?php endif; ?>
    
      <?php endforeach; ?>
    
    <?php endif; ?>

    It’s not the best code, but it works. If I later have the time and energy, I might change my code to a cleaner version based on your input.

    Thanks again,
    Patrick

  • You could use multiple fields, probably in a repeater, where you supply all the information. For example a “URL” field, a link “Text” field and an field to enter the icon you want to use.

    Yes, I thought about that but I don’t want/need to input new information every time I create a post. The choices and their values should be pre-defined. Every time I choose “WordPress”, for instance, it should show the WordPress Icon followed by the word WordPress as a link on the front end. I can’t do that with the repeater field, can I?

    You could also code the icons and url into your template based on the values selected, but your code would need to supply the icon and the url information.

    That would be ideal and is basically what I’m looking for. But how can I achieve this? Since the checkbox field only provides one value per choice, I would have to find a way to check if that value matches a certain string and if so, prepend the code for the icon and wrap the value into the right link tag. I’m sure this would be possible with PHP, but I’m not that good at it and I’m pretty sure that it would lead to very long code in the template, right?

    If I would just have the option to assign the three variables icon word, url, and name to each choice and if it would provide those three values separately instead of just as one single value, then I would be comfortable to do the rest in the template.

  • You could use multiple fields, probably in a repeater, where you supply all the information. For example a “URL” field, a link “Text” field and an field to enter the icon you want to use.

    Yes, I thought about that but I don’t want/need to input new information every time I create a post. The choices and their values should be pre-defined. Every time I choose “WordPress”, for instance, it should show the WordPress Icon followed by the word WordPress as a link on the front end. I can’t do that with the repeater field, can I?

    You could also code the icons and url into your template based on the values selected, but your code would need to supply the icon and the url information.

    That would be ideal and basically what I’m looking for. But how can I achieve this? Since the checkbox field can only provide one value per choice, I would have to find a way to check if that value matches a certain string and if so, prepend the code for the icon and wrap the value into the right link tag. I’m sure this would be possible with PHP, but I’m not that good at it and I’m pretty sure that it would lead to very long code in the template, right?

    If I would just have the option to assign the three variables icon word, url, and name to each choice and if it would provide those three values separately instead of just as one single value, then I could query these values and wrap them appropriately into i and a tags in the template.

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