I am using the custom field plugin and i have made some custom fields in wordpress.
I wrote php to bring to the front the custom fields using the following code, but it doesn’t stop looping and I need only 10 or 20 times to loop.
So I want to change but I don’t know how.
<?php if( $banner_query2->have_posts() ): ?>
<?php while ( $banner_query2->have_posts() ) : $banner_query2->the_post(); ?>
<?php
$post_id = get_the_ID();
?>
<?php
$featured_matches = get_field('locations');
if( $featured_matches ): ?>
<ul>
<?php foreach( $featured_matches as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<!-- <a href="<?php //the_permalink(); ?>"><?php //the_title(); ?></a> -->
<span><?php the_field( 'customfile' ); ?></span>
<span><?php the_field( 'customfile2' ); ?></span>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
2
Answers
Seems like in wordpress if you add just
break;
in the end of the foreach loop works fineIts quite possible to terminate a foreach loop or any other early, by using
break;
so all you need to do is maintain a counter and when it reaches the magic numberbreak
the loop.