WordPress.org

Ready to get started?Download WordPress

Forums

Strange $poststoshow and static $count behaviour (1 post)

  1. heriz
    Member
    Posted 2 years ago #

    I have a column which pulls in the 6 latest posts in a category (shown by a custom image/title), with a separating line between every two. Something weird is happening where the count is broken on the 4th post, and I can't work out why. Here's the code:

    <?php
    	$poststoshow = 2;
    	if(is_single()) { $currentpost = get_the_ID(); }
    
    	$posts = get_posts('numberposts='.$poststoshow.'&category=99'); ?>
    
    	<?php
    	foreach ($posts as $post) { 
    
    		setup_postdata($post);
    		static $count1 = 0; 
    
    		if ($count1 == "2") {
    			break;
    		}
    		else { ?>
    			<div class="packshot">FIRST ROW POSTS</div>
    
    			<?php
    			//the_content();
    			$count1++;
    		}
    	} ?><hr class="break" />
    
    	<?php
    	unset($posts);
    	$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count1.'&category=99');
    	?>
    
    	<?php
    	foreach ($posts as $post) {
    
    		setup_postdata($post);
    		static $count2 = 2;
    		if ($count2 == "4") {
    			break;
    		}
    		else { ?>
    			<div class="packshot">SECOND ROW POSTS</div>
    			<?php
    			//the_content();
    			$count2++;
    		}
    	}
    	?><hr class="break" />
    
    	<?php
    	unset($posts);
    	$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count2.'&category=99');
    	?>
    
    	<?php
    	foreach ($posts as $post) {
    
    		setup_postdata($post);
    		static $count3 = 4;
    		if ($count3 == "6") {
    			break;
    		}
    		else { ?>
    			<div class="packshot">THIRD ROW POSTS</div>
    			<?php
    			//the_content();
    			$count3++;
    		}
    	}
    	?>				<hr class="break" />

    With apologies for layout - wasn't sure how it'd render here.

    So on the 4th post (on the second line), it simply isn't pulled in. Then, no posts appear after that. If I change the third row code to

    foreach ($posts as $post) {
    
    		setup_postdata($post);
    		static $count3 = 6;
    		if ($count3 == "8") {
    			break;
    		}

    Then posts are pulled in, but two are missing in the middle.

    Changing the static $count pulls in different posts but no matter what I do, there always seems to be one missing. Compare this with the same code used to pull in rows of 3, elsewhere on the site:

    <?php
    $poststoshow = 3;
    if(is_single()) { $currentpost = get_the_ID(); }
    
    $posts = get_posts('numberposts='.$poststoshow.'&category=3'); ?>
    
    <?php
    foreach ($posts as $post) { 
    
    	setup_postdata($post);
    	static $count1 = 0; 
    
    	if ($count1 == "3") {
    		break;
    	}
    	else { ?>
    		<div class="packshot">FIRST ROW POSTS</div>
    
    		<?php
    		//the_content();
    		$count1++;
    	}
    } ?><hr class="break" />
    
    <?php
    unset($posts);
    $posts = get_posts('numberposts='.$poststoshow.'&offset='.$count1.'&category=3');
    ?>
    
    <?php
    foreach ($posts as $post) {
    
    	setup_postdata($post);
    	static $count2 = 3;
    	if ($count2 == "6") {
    		break;
    	}
    	else { ?>
    		<div class="packshot">SECOND ROW POSTS</div>
    		<?php
    		//the_content();
    		$count2++;
    	}
    }
    ?><hr class="break" />
    
    <?php
    unset($posts);
    $posts = get_posts('numberposts='.$poststoshow.'&offset='.$count2.'&category=3');
    ?>
    
    <?php
    foreach ($posts as $post) {
    
    	setup_postdata($post);
    	static $count3 = 6;
    	if ($count3 == "9") {
    		break;
    	}
    	else { ?>
    		<div class="packshot">THIRD ROW POSTS</div>
    		<?php
    		//the_content();
    		$count3++;
    	}
    }
    ?>

    This works perfectly.

    Any ideas? I must be doing something wrong somewhere along the line...

Topic Closed

This topic has been closed to new replies.

About this Topic