ST80
ST80

Reputation: 3893

Wordpress ACF How to display values based on selected value

I have a field of type Repeater where each row has a sub_field of type select with two values: "cd" and "vinyl".

Now I hae two templates where I want to display All "Cds" and all "Vinyls" - how can I achieve that?

Here is my loop:

<?php if( have_rows('category_group') ): ?>

     <ul class="product__categories">

         <?php while( have_rows('category_group') ): the_row(); 


              // vars
              $title= get_sub_field('title');
              $image = get_sub_field('image');
              $product_type = get_sub_field('product_type');

              ?>

              <li class="product__categories--category">

                <a href="/products/<?php echo strtolower($title); ?>">
                  <div class="wrapper" style="background: url('<?php echo $image['url']; ?>'); background-size: cover; background-position: center; background-repeat: no-repeat">
                  <?php } ?>
                    <h1"><?php echo $title; ?></h1>
                  </div>
                </a>

              </li>

            <?php endwhile; ?>
      </ul>

Do I hae to do something like:

$type = get_sub_field('product_type');

if ($type == "vinyl") ....

Upvotes: 1

Views: 58

Answers (1)

Rishit Patel
Rishit Patel

Reputation: 96

you should try this :

I have a field of type Repeater where each row has a sub_field of type select with two values: "cd" and "vinyl".

Now I hae two templates where I want to display All "Cds" and all "Vinyls" - how can I achieve that?

Here is my loop:

<?php if( have_rows('repeater_field_name') ): ?>

     <ul class="product__categories">

         <?php while( have_rows('repeater_field_name') ): the_row(); 


              // vars
              $title= get_sub_field('title',get_the_ID());
              $image = get_sub_field('image',get_the_ID());
              $product_type = get_sub_field('product_type',get_the_ID());
              echo $product_type; 

              ?>

              <li class="product__categories--category">

                <a href="/products/<?php echo strtolower($title); ?>">
                  <div class="wrapper" style="background: url('<?php echo $image['url']; ?>'); background-size: cover; background-position: center; background-repeat: no-repeat">
                  <?php } ?>
                    <h1"><?php echo $title; ?></h1>
                  </div>
                </a>

              </li>

            <?php endwhile; ?>
      </ul>
<?php endif; ?>

Also If you want to check for specific value then :

if ($product_type == "vinyl"){
   echo $product_type;
}

Upvotes: 1

Related Questions