Support

Account

Home Forums General Issues Outputting checkboxes as unordered list

Solving

Outputting checkboxes as unordered list

  • I am having issues with trying to output an UL from checkboxes in ACF Pro. I have followed the instructions on this page https://www.advancedcustomfields.com/resources/checkbox/ with no resolution. I’m not sure what I am doing wrong here. My field name is “site_features” and I am working with this code added to my functions.php file:

    $site_features = get_field('site_features');
    if( $site_features ): ?>
    <ul>
        <?php foreach( $site_features as $site_features ): ?>
            <li><?php echo $site_features; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    Where am I screwing this up?

  • 
    foreach( $site_features as $site_features ):
    

    second variable name needs to be different than the first

  • So, excuse my newbie-ness, but what variable am I supposed to use and where do I get it from?

  • 
    $site_features = get_field('site_features');
    if( $site_features ): ?>
    <ul>
        <?php 
    // notice the missing "s"
    foreach( $site_features as $site_feature ): ?>
            <li><?php echo $site_feature; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    

    What does this do?

    
    foreach( $site_features as $site_features ):
    

    The first iteration of this loop sets $site_features (an array) to the string of whatever is in the first array value. This is the same as doing

    
    $site_features = $site_features[0]
    

    and this breaks your loop. Lets say that the first value of that array is “Some String”, then the next itteration will set $site_features equal to the second character of the string.

  • Yes, I did try that earlier today, with no luck. Was just trying to follow what that page link said and I noticed their missing “s” in some places. At this point, ACF has entirely quite offering any output at all in my template. (unordered or otherwise) I have obviously flubbed something up.

  • Other than what I pointed out I don’t see anything wrong with the bit of code that you posted.

    Is your checkbox field a sub field?

    Where is this loop that is not working, it is inside of a WP have_posts() loop?

    Will need a bigger picture of your fields setup and code.

  • Sure. I understand. This is the way I have the field setup:

  • I was able to bring the field back and get it to finally show up on the front end, but this is how it is outputting (with commas):

    I’m hoping to output that as an unordered list instead.

  • This is how I have the code in the functions.php file:

    $site_features = get_field('site_features');
    if( $site_features ): ?>
    <ul>
        <?php 
    // notice the missing "s"
    foreach( $site_features as $site_feature ): ?>
            <li><?php echo $site_feature; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
  • Your code is in functions.php? How is this code called? Is it in a function? This is usually not the correct place to put this code, it would normally go in the template file where you want it to appear.

  • Gotcha. Okay that makes more sense. LOL. So, I had tried putting it on the template before with no luck. Now I get my UL dot, but no values are pulled in. My HTML is:

    <h3>Website Features</h3>
    <?php
    $site_features = get_field('site_features');
    if( $site_features ): ?>
    <ul>
        <?php 
    foreach( $site_features as $site_feature ): ?>
            <li><?php echo $site_feature; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    And, I get this:

  • And thank you so much for your suggestions… you are helping me think of things I haven’t tried yet.

  • I still don’t see why it’s not working.

    Where are these fields entered? Post? Page? CPT? Taxonomy Term? Options Page?

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

You must be logged in to reply to this topic.