Loop through ACF to display all possible field values?

I am building a site that is using isotope to filter through posts on a page.

I’m using advanced custom fields (http://www.advancedcustomfields.com/) and have created a section where the user can set a field for ‘Project Difficulty’.

Read More

I am trying to loop through all of the possible selections to create a list of links that a user can click to sort through (using isotope). I’ve got this successfully working using ‘tags’ , but I don’t want to tag each project with the difficulty level, I want the user to select it when creating the post in an ACF drop down.

To successfully get and display a list of tags in the form of links, I’ve used this code:

<?php
$tags = get_tags();
  $html = '<div class="post_tags">';
  foreach ( $tags as $tag ) {
  $tag_link = get_tag_link( $tag->term_id );

  $html .= "<a data-filter=.{$tag->name} title='{$tag->name} Tag' class='{$tag->slug}'>";
  $html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html; 
?>

Now I’ve tried to alter that to get it working with ACF using some code like this:

  <?php
   $fields = get_fields();
   $html = '<div class="post_tags">';
  foreach ( $fields as $field ) {
     $tag_link = get_fields( $field->task_difficulty );

    $html .= "<a data-filter=.{$field->name} title='{$field->name} Tag' class='{$field->slug}'>";
    $html .= "{$field->name}</a>";
  }
   $html .= '</div>';
   echo $html;
  ?>

But what it is outputting is:

<a data-filter="." title=" Tag" class=""></a>

and it’s not adding any of the correct data. Clearly some of my values are off. How can I loop through the possible options, and add them as a link as I did with the tags?

Thanks

Related posts

Leave a Reply

1 comment

  1. Anyone else who is having difficulty with this, I resolved the issue by doing the following:

            // must add field key of the field you want
            $field_key = "field_52a087a80a4c6";
            $field = get_field_object($field_key);
    
            if( $field )
            {
                echo '<div class="acf-task-difficulty-values">';
                    foreach( $field['choices'] as $k => $v )
                    {
                        echo '<a data-filter=.'.$k.' onclick="return false;">' . $v . '</a>';
                    }
                echo '</div>';
            }
    

    You can then style it in your CSS file.