Forums

Need help with a foreach on every 3 posts (6 posts)

  1. vektordesign
    Member
    Posted 3 years ago #

    I started from the theme Hemingway and Kyle used a $first = true; php variable to take the left margin off of the first post on the index with a CSS class of first. I am trying to make a fairly complex layout with a few instances of The Loop but what I need is a php counter of some sort that adds a class of first (or any class for that matter) on the first post and subsequently on every 3rd post after that. I know this is a php question but this being such a basic concept in php, the documentation is slim and general.
    This is what the $first variable corresponds to:

    <div id="<?php the_ID(); ?>" class="story photography_post<?php if($first == true) echo " first" ?>">

    The main idea being that I can have aligned margins on both sides while running a 3 column post setup no matter how many posts I put up there.

    Thanks in advance!

  2. vektordesign
    Member
    Posted 3 years ago #

    Ok so I saw a great page on phparadise about arrays, which is what I assume I need to use, and I think I need to add an array to my foreach ($posts as $post) : start_wp(); ?> dealio. any help would be killer.

  3. xdesi
    Member
    Posted 3 years ago #

    I'm not great on PHP so I can't really help you out here, but if you get really stuck you could just do multiple loops e.g show the first three posts, stop the loop, and do another loop with three posts, whilst using the first tecnhique you are currenlty using so that it gets added to the third post each time.

    Another possiblity is that you could also maybe implement a counter variable which increments each time, and then use an if statement to say if this variable equals a multiple of 3 then give it this class.

    However both methods are probably quite long winded but they are the only solutions i can provide with my limited knwoledge.

  4. vektordesign
    Member
    Posted 3 years ago #

    @xdesi, Thanks for the reply. Multiple loops definitely seem to be a solution, however I actually have like... 5 loops right now anyway and didn't know what kind of overhead that many loops would incur. Let me dig up my code and I'll post it: http://pastie.org/270585

    Basically I have 5 loops, consisting of 4 category pulls, each in their own div that slides out on js call, and then the last loop calls 2 regular posts, or will call 2 regular posts excluding the categories 24, 25, 26, 27.

    The four category loops need to have some sort of counter that cycles through a foreach array or something like 'first','',''.

    But I think I'm in over my head.

  5. vektordesign
    Member
    Posted 3 years ago #

    So I read a ton of php documentation and learned some stuff and came up with this, CHECK IT OUT!

    <?php query_posts('cat=27'); ?>
    	<?php $i = 1 ?>
    	<?php $posts = get_posts('category=27');
    	foreach ($posts as $post) : start_wp(); ?>
    	<div id="<?php the_ID(); ?>" class="story photography_post<?php if($i == 1) echo " first";  ?>">
    		<div class="postwrapper">
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    			<?php $hemingway->excerpt() ?>
    			<div class="details">
    				<?= _('Posted at') ?> <?php the_time('ga \o\n ' . $hemingway->date_format(true) . '/y') ?> | <?php comments_popup_link('no comments', '1 comment', '% comments'); ?> | Filed Under: <?php the_category(', ') ?> <span class="read-on"><a href="<?php the_permalink() ?>">read on</a></span>
    			</div> <!-- close: details -->
    		</div> <!-- close: postwrapper -->
    	</div> <!-- close: <?php the_ID(); ?> -->
    	<?php if($i == 3){$i = 1;} else {$i++;} ?>
    	<?php endforeach; ?>

    There it is. Loops the foreach and puts a first class on 1st and every 3 posts after that. Hope this helps someone.

  6. marresc
    Member
    Posted 3 years ago #

    It sure does. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic