如何打破While循环?

时间:2017-10-25 19:03:39

标签: php

有人写了这个PHP代码并最终离开了公司。我是一名新员工,我试图用他编写的这段代码打破一段时间。现在,它在一个页面上循环播放了9篇视频文章。我试图打破循环并限制为5个视频文章并添加一个"查看更多视频!"链接并在第二页上显示剩下的视频,而不会破坏他写的代码。

代码:

<?php
$count = 0;
$limit = 5;

                if ( have_posts() ) : while ($count < $limit && have_posts()) : the_post();

                    $this_timestamp = date('l F j, Y', strtotime($post->post_date));
                    $this_categories = wp_get_post_categories( $post->ID );
                    $this_excerpt = $post->post_content;
                    $this_featuredVideo = get_post_meta($post->ID, 'post_featuredVideo', true);
                    $this_featuredVideoTitle = get_post_meta($post->ID,'post_featuredVideoTitle',true);
                    $this_gallery = get_post_meta($post->ID, 'post_imageGallery', true);

++count;
            ?>

<article class="unum_article">
                    <header class="unum_article-header clearfix">
                        <time class="unum_article-header_timestamp"><?php echo $this_timestamp; ?></time>
                    </header>
                    <figure class="unum_article-media">
                    <?php if ($this_featuredVideo && !($this_gallery)): ?>

                        <div class="flowplayer fp-outlined is-splash fp-mute" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID,'full');?>)"  data-analytics="UA-2775729-22" data-key="$845219950943213,$437146226347740"  data-share="false" title="<?php echo $this_featuredVideoTitle; ?>">
                            <video >
                                <source src="<?php echo $this_featuredVideo; ?>" type="video/mp4">Your browser does not support the video tag.</source>
                            </video>
                        </div>

                    <?php else: ?>

                        <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post->ID,'full',array('class' => "unum_article-media_content")); ?></a>

                    <?php endif; ?>
                    </figure>
                    <h2 class="unum_article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="unum_article-content">
                        <p><?php echo the_content('Read More', true); ?></p>
                    </div>
                    <ul class="unum_article-tags">
                    <?php
                        foreach($this_categories as $cat):
                            $category = get_category($cat);
                    ?>
                        <li><a href="<?php echo esc_url(get_category_link($cat)); ?>"><?php echo $category->name; ?></a></li>
                    <?php endforeach; ?>
                    </ul>
                </article>

            <?php endwhile; ?>

2 个答案:

答案 0 :(得分:1)

简单

$count = 0;
$limit = 5;

if ( have_posts() ) : 
    while ($count < $limit && have_posts() ) : 
        the_post();
        ...
        ++$count;
    endwhile;
endif;

另请注意have_posts()应该排在第二位,因为这是更加耗时的检查。因此,当限制失败时,它不需要检查更多帖子。另一种方法是做一个不必要的函数调用。

它也值得$limit一个变量,后者更改它更清晰,但这是我的意见。

从技术上讲,将这些变量放在if块中可能更好,但无论如何。

答案 1 :(得分:1)

正如你已经控制的那样,如果has_post是空的,你不需要它作为你的时间条件,所以如果是我,我会这样做:

<?php
 if ( have_posts() ) {
                $count = 0;
                $limit = 5; 
                while ( $count < $limit ) { 
                the_post();
                $this_timestamp = date('l F j, Y', strtotime($post->post_date));
                $this_categories = wp_get_post_categories( $post->ID );
                $this_excerpt = $post->post_content;
                $this_featuredVideo = get_post_meta($post->ID, 'post_featuredVideo', true);
                $this_featuredVideoTitle = get_post_meta($post->ID,'post_featuredVideoTitle',true);
                $this_gallery = get_post_meta($post->ID, 'post_imageGallery', true);
                $count++;
        ?>

            <article class="unum_article">
                ...[Your code]
            </article>

        <?php } 
    }?>