Support

Account

Home Forums Add-ons Repeater Field ACF foreach for loop, content being repeated for each custom taxonomy

Solved

ACF foreach for loop, content being repeated for each custom taxonomy

  • Alrighty…
    So using ACF, I have created a repeater that contains sub_fields ‘name’, ‘image’, ‘description’, ‘file’ and ‘category’ (‘category’ being a custom taxonomy)

    Using a basic php foreach loop I have successfully managed to get the all the sub_fields to work, except for the custom taxonomy ‘category’. In the #downloads-container I have my for($i = 0; $i < count($row[‘category’]); $i++) to get all the custom taxonomy options, but of course because my echo to display everything is in there, I’m seeing the content getting repeated for each category they are selected for.

    Can anyone help me figure out the best way to write this?

    <?php
    /*
      Template Name: Assets Template
    */
    get_header(); ?>
    
              <h1><?php echo get_the_title(); ?></h1>
    
              <hr>
    
              <!-- Start the Loop. -->
              <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
                <div class="controls">
                  <?php
                    $terms = get_terms( 'topics', array('hide_empty' => false) );
                    $filter_links = array();
                    foreach ( $terms as $term ) {
                      $filter_links[] = '<button class="filter" data-filter=".'.$term->name.'">'.$term->name.'</button>';
                    }
                    $on_filter = join( "", $filter_links );
                  ?>
                  <label>Filters: </label><button class="filter" data-filter="all">All</button><?php echo $on_filter; ?>
    
                </div><!-- end .controls -->
                <div class="clear"></div>
    
                <div id="downloads-container">
                  <div class="container">
                    <?php
                      $rows = get_field('asset');
    
                      if($rows) {
    
                        foreach($rows as $key => $row) {
                          $column_id[$key] = $row['id'];
                        }
                        array_multisort($column_id, SORT_ASC, $rows);
    
                        foreach($rows as $row) {
                          for($i = 0; $i < count($row['category']); $i++) {
                            $taxonomyterm = $row['category'][$i] -> name;
                            echo '<div class="mix img ' . $taxonomyterm . '"><img src="' . $row['image'] . '"/><h4>' . $row['name'] . '</h4>' . $row['description'] . '<div class="overlay"><a href="' . $row['file'] . '" class="expand" download>↓</a><a class="close-verlay hidden">x</a></div></div>';
                          }
                          
                        }
                      }
                    ?>
                  </div>
                  <div class="gap"></div>
                  <div class="gap"></div>
                </div>
    
                <?php endwhile; else : ?>
    
                  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
              <?php endif; ?>
    
    <?php get_footer(); ?>
  • Solved my own problem:

    <?php
    /*
      Template Name: Assets Template
    */
    get_header(); ?>
    
              <h1><?php echo get_the_title(); ?></h1>
    
              <hr>
    
              <!-- Start the Loop. -->
              <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
                <div class="controls">
                  <?php
                    $terms = get_terms( 'asset-categories', array('hide_empty' => false) );
                    $filter_links = array();
                    foreach ( $terms as $term ) {
                      $filter_links[] = '<button class="filter" data-filter=".'.$term->name.'">'.$term->name.'</button>';
                    }
                    $on_filter = join( "", $filter_links );
                  ?>
                  <label>Filters: </label><button class="filter" data-filter="all">All</button><?php echo $on_filter; ?>
    
                </div><!-- end .controls -->
                <div class="clear"></div>
    
                <div id="downloads-container">
                  <div class="container">
    
                    <?php
                      $rows = get_field('asset');
     
                      if($rows) {
     
                        foreach($rows as $key => $row) {
                          $column_id[$key] = $row['id'];
                        }
                        array_multisort($column_id, SORT_ASC, $rows);
     
                        $i = 0;
                        foreach($rows as $row) {
                            $taxonomyterm="";
                            for($i = 0; $i < count($row['category']); $i++) {
                                    $taxonomyterm = $taxonomyterm . $row['category'][$i] -> name . ' ';
                            }
                          echo '<div class="mix img ' . $taxonomyterm . '">' . (($row['image'])?) . '<img src="' . $row['image'] . '"/><h4>' . $row['name'] . '</h4>' . $row['description'] . '<div class="overlay"><a href="' . $row['file'] . '" class="expand" download>↓</a><a class="close-verlay hidden">x</a></div></div>';
                          $i++;
                        }
                      }
                    ?>
                  </div>
                  <div class="gap"></div>
                  <div class="gap"></div>
                </div>
    
                <?php endwhile; else : ?>
    
                  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
              <?php endif; ?>
    
    <?php get_footer(); ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF foreach for loop, content being repeated for each custom taxonomy’ is closed to new replies.