0

i want to show only the first element of the "type" and not all elements which have the type "payment-services". How can i do that? Hope somebody can help me...

<?php if( have_rows('events', 4127) ):
                    while ( have_rows('events', 4127) ) : the_row();

                        $types = get_sub_field('type');

                        foreach($types as $type) {
                            if ($type == "payment-services") { ?> 



                        <div class="item-<?php foreach($types as $type ): echo $type; echo ' ' ; endforeach; ?>">
                            <img class="events-images" src="<?php the_sub_field('image', 4127); ?>" alt="">
                            <h2 class="events-title"><?php the_sub_field('title', 4127); ?></h2>
                            <p class="events-date"><?php the_sub_field('date', 4127); ?></p>
                            <p class="events-location"><?php the_sub_field('location', 4127); ?></p>
                            <a class="events-url btn" href="<?php the_sub_field('url', 4127); ?>"><?php the_sub_field('url_text'); ?></a>
                        </div>

                         <?php break; ?>
                    <?php }
                        }
                endwhile;
            endif; ?>
0

If I understand this correctly, you want the whole code inside if ($type == "payment-services") to run only once.
There might be an easier way to do this, but a suggestion is:

  1. Add a variable with a certain value before your for loop (e.g. $run="yes")
  2. Put the code you only want to run the first time inside an if statement, where it only runs if the above variable has its initial value
  3. Change the value of the variable during the first iteration so the whole if statement is false during the rest of the loop.

So your code could look something like this:

....
$run="yes";
foreach($types as $type) {
  if($run=="yes"){
    if ($type == "payment-services") { 

      rest of code which will only run once

      $run="no";
    }       
  }
}
....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.