Support

Account

Home Forums Front-end Issues Loop through all possible choices dropdown

Solved

Loop through all possible choices dropdown

  • 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
    $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; 
    ?>

    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?

    I have also tried this:

    $values = get_field_object('task_difficulty');
    			if(is_array($values)) {
    				echo '<ul class="acf-task-difficulty-values">';
    			 
    				foreach($values as $value)
    				{
    					echo '<li>' . $value . '</li>';
    				}
    			 
    				echo '</ul>';
    			}

    But it only displays the selected option for the first post on the page. It doesn’t loop through each post and display a link for the possible option.

    Thanks

  • To anyone else looking for the solution I was able to execute this by using the following code:

      // 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>';
            }
  • Hi @eherman24

    Thanks for posting your solution.

    Thanks
    E

  • hi eherman24

    thanks a lot , would you have any solution for a custom taxonomy ?

    thnaks

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

The topic ‘Loop through all possible choices dropdown’ is closed to new replies.