簡體   English   中英

如何中斷While循環?

[英]How do I break a While Loop?

有人寫了這個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; ?>

簡單

$count = 0;
$limit = 5;

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

還要注意have_posts()應該排在第二位,因為這是比較耗時的檢查。 因此,如果限制失敗,則無需檢查更多帖子。 圍繞它的另一種方式則對此做了不必要的函數調用。

$limit設為變量也很值得,以后更容易更改它,但這是我的觀點。

從技術上講,最好將這些變量放在if塊內,但無論如何。

因為您已經控制have_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 } 
    }?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM