Tiffany Israel
Tiffany Israel

Reputation: 470

PHP query stop after count reached

I can't seem to figure this out from the php manual. The following code is giving me the 2nd post that is returned. What I would like to do is get 2 posts and then stop.

$count=0;
    $related = p2p_type($connected_type)->get_related( get_queried_object_id() );

            // Display related posts
            if ( $related->have_posts() ) :
            $count++;
            while ( $related->have_posts() ) : $related->the_post();            
        if ($count == 2) {

        echo the_title();   

        } else {

        echo 'this post won't show up'; 
        }

    endwhile; 

                // Prevent weirdness
            wp_reset_postdata();

            endif;  

Upvotes: 1

Views: 388

Answers (1)

NullPoiиteя
NullPoiиteя

Reputation: 57322

you can use if($count >2) break; or break; in the else

break ends execution of the current while .

for more information check this link php manual break

Upvotes: 2

Related Questions