• Resolved csleh

    (@csleh)


    This is the current code. The counter goes up to 3 then resets to 1, and the article is given the appropriate class. Works fabulously.

    <?php $counter = 0; ?>
    <?php
    if(have_posts()) : while(have_posts()) : the_post();
     <?php $counter++;  ?>
     <article class="th<?php echo $counter; ?>">
    	<?php // reset counter if 3 has been reached
    		if ($counter==3){
    		$counter=0;
    		} ?>
        <!--stuff-->
    </article>
    <?php } endwhile; endif;
    ?>
    <article class="th1">
        <!--stuff for an extra article-->
    </article>

    Now I have to pull in one extra article. it isn’t part of the loop because it’s from a different area. Right now it has a class of 1 all the time but what should happen is that it should flow with the rest. It doesn’t matter if it’s first or last. So I think the problem is either:

    – the first loop starts with 2, goes to 3, then goes back to 1, 2, 3, until the posts are finished.

    OR

    – loop goes 1, 2, 3 until all posts are finished. Then figure what was the last number, and add one to this article outside the loop.

    Is either of these possible? Is there an easier way?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    (@alchymyth)

    is the article always either first before or last after the loop?
    is it just one article?

    have you tried (same code for before the loop and for after the loop):

    <article class="th<?php echo $counter++; ?>">
    Dion

    (@diondesigns)

    I’m amazed that code works at all! PHP should abort with a parse error at this line:

    <?php } endwhile; endif;

    due to the extraneous brace. In any case, since the $counter variable is set properly when exiting the loop, you should be able to use the following line for the external article:

    <article class="th<?php echo $counter; ?>">
    Thread Starter csleh

    (@csleh)

    Both of these solutions
    <article class="th<?php echo $counter++; ?>">
    <article class="th<?php echo $counter; ?>">
    result in a number 0. I’m looking for 1, 2 or 3.

    Yes, always just one article, always the same one (it’s pulling from a custom field on a different page). It can be either before the first loop or after the last, whichever is easiest.

    ah, I took out a line when stripping out extraneous, that’s why the end bracket seemss wrong. This is the actual code

    <?php $counter = 0; ?>
    <?php if(have_posts()) : while(have_posts()) : the_post();
      if (($post->post_parent) ) {
              continue;
          } else { ?>
     <?php $counter++;  ?>
    <article class="th<?php echo $counter; ?> m1">
    	<?php // reset counter if 3 has been reached
    		if ($counter==3){
    		$counter=0;
    		} ?>
    <!-- stuff with posts happens here-->
    </article>
    <?php } endwhile; endif;
    ?>
    <article class="th<?php echo $counter; ?> m1">
    <!-- other stuff with this special post from elsewhere-->
    </article>

    Dion

    (@diondesigns)

    If the custom article can be at the top, then the following should work:

    <article class="th1 m1">
    	<!-- other stuff with this special post from elsewhere-->
    </article>
    <?php
    $counter = 1;
    if (have_posts()) {
    	while (have_posts()) {
    		the_post();
    		if ($post->post_parent) {
    			continue;
    		} else {
    			$counter++;
    ?>
    			<article class="th<?php echo $counter; ?> m1">
    				<!-- stuff with posts happens here-->
    			</article>
    <?php
    			$counter = $counter % 3;
    		}
    	}
    }
    ?>
    Thread Starter csleh

    (@csleh)

    that works perfectly, thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘counter trickery – adding an extra bit from outside the loop but in sequence’ is closed to new replies.