简体   繁体   中英

How do I break a While Loop?

Somebody wrote this php code and left the company eventually. Im a new employee and Im trying to break a while loop with this code he wrote. Right now, it loops 9 video articles on a single page. Im trying to break the loop and limit to 5 video articles and add a "See more videos!" link and show the rest of the video on the second page without breaking the code he wrote.

Code:

<?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; ?>

Simple

$count = 0;
$limit = 5;

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

Also note have_posts() should come second, because that is the more time intensive check. So when the limit fails it doesn't need to check for more posts. The other way around it does an unnecessary function call for that.

its also worth making $limit a variable it's much cleaner to change it latter, but that is my Opinion.

Technically its probably better to put those variables inside the if block but whatever.

As you already control if have_post is empty you don't need it as a condition in your while so if it was me I'll do it like this :

<?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 } 
    }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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