我正在使用自定义字段插件,并且我在wordpress中制作了一些自定义字段。
我使用下面的代码编写php来把自定义字段放在前面,但是它不会停止循环,我只需要循环10到20次。
所以我想改变,但我不知道如何改变。
<?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(). ?>发布于 2021-04-23 17:16:05
在wordpress中,如果您只在foreach循环的末尾添加break;,就可以很好地工作。
发布于 2021-04-22 20:47:34
通过使用break;,可以提前终止一个foreach循环或其他任何循环,这样您所需要做的就是维护一个计数器,当它达到神奇的数字时,就会break循环。
<?php
$cnt = 0;
$maxcnt = 10;
foreach( $featured_matches as $post ):
// do your funky stuff
$cnt++;
if ( $cnt >= $maxcnt ) :
break;
endif;
endforeach;https://stackoverflow.com/questions/67213399
复制相似问题