Viewing 3 replies - 1 through 3 (of 3 total)
  • The problem I see is with how you’re using $counter. You start by defining $counter=0. Then immediately increment by 1 inside the loop. This should start you off with a class of “fl” for your first element … but you never reset the counter and keep incrementing by 1 in each loop. So, if you loop through 5 posts, $counter should be 1, 2, 3, 4, 5 and your class should be fl, fr, fr, fr, fr respectively.

    I use a similar system on my site to alternate classes … here’s a snipped from my own loop (maybe it will help)

    <?php
    $i=1;
    if (have_posts()) : while (have_posts()) : the_post();
    	$i++;
    	$class = ($i&1) ? 'odd-' : 'even-';
    	?>
    	<div class="<?php echo $class; ?>olderpost">
    	<h3 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    	<div class="content">
    		<?php the_excerpt(); ?>
    	</div>
    	</div><!-- /olderpost -->
    <?php	endwhile;
    endif;
    ?>
    Thread Starter thinkofmedia

    (@thinkofmedia)

    I amended my code similar to yours. But still do not get the alternating classes.

    Can you see what’s missing.

    <?php get_header(); ?>
    
    	<!-- Featured Slider -->
    	<?php include (TEMPLATEPATH . "/includes/slider.php"); ?>
    	<!-- Featured Slider end -->
    
    <div id="main-content" class="home">
       	 <div class="content">
    				<div class="col-left">
    						<div id="main">
    
                    <!-- Post Starts -->
                   <div class="box">
    
    				<?php
                        // Split the main content pages from the options, and put in an array
                        $featpages = get_option('woo_main_pages');
                        $featarr=split(",",$featpages);
                        $featarr = array_diff($featarr, array(""));
    
    				 foreach ( $featarr as $featitem ) {
                    query_posts('page_id=' . $featitem);
    
            $i=1;
    		if (have_posts()) : while (have_posts()) : the_post();
    	$i++;
    	$class = ($i&1) ? 'fl' : 'fr';
    	?>
    
    	<div class="post<?php echo $class; ?>">
                    	<div class="box-post-content">	
    
    <h2><a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    
    					<?php if ( get_post_meta($post->ID, 'image', true) ) { ?>
                        <img src="<?php echo get_post_meta($post->ID, "image", $single = true); ?>" alt="" class="home-icon" />
    					<?php } ?> 
    
                           <?php the_excerpt(); ?>
                           <a href="<?php the_permalink() ?>">Read more</a></p>        
    
           				 </div> <!-- box post -->
    
                    <?php endwhile; endif; ?>
                    </div> <!-- Post Ends -->
                    <?php } ?>
    
                 </div>
                    <!-- box Ends -->
    
    			<?php //endwhile; endif; ?>  
    
               				 </div><!-- main ends -->
           			 </div><!-- .col-left ends -->
    
            <?php get_sidebar('home'); ?>
    
        </div><!-- .content Ends -->
    </div><!-- #main-content ends -->
    
    <?php get_footer(); ?>

    Looks like you are missing an </div> just after <!-- box Ends -->

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Class not adding via loop’ is closed to new replies.