Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

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

like image 820
EHerman Avatar asked Dec 05 '13 15:12

EHerman


People also ask

How do you display field value of ACF?

To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field. To display a field, use the the_field() in a similar fashion.

How do you get the Repeater field value in ACF?

Link to heading#Parametersthe_repeater_field( $field_name, $post_id ); $field_name (string) (Required) The name of the Repeater field to be retrieved. e.g. 'gallery_images' $post_id (mixed) (Optional) The post ID where the value is saved.

How do I make a custom field value Repeater in WordPress?

Working with the repeater field is relatively straightforward – you just need to click the “Add Row” button to add a new row and edit the values of the subfields that are shown.


1 Answers

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.

like image 163
EHerman Avatar answered Oct 02 '22 04:10

EHerman