繁体   English   中英

将 wordpress 循环限制为 10 或 20 个柱子

[英]Limit wordpress loop to 10 or 20 posts

我正在使用自定义字段插件,并且在 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(). ?>

很可能通过使用break; 所以你需要做的就是维护一个计数器,当它达到幻数时break循环。

<?php 
    $cnt = 0;
    $maxcnt = 10;
    foreach( $featured_matches as $post ): 

        // do your funky stuff

        $cnt++;
        if ( $cnt >= $maxcnt ) :
            break;
        endif;
    endforeach;

好像在 wordpress 如果你添加只是break; 在 foreach 循环结束时工作正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM