• Hi there,

    Me again with another little issue.

    I am calling posts from a specific category in my custom theme. This all works fab.

    Just one thing I wanted to do is to add a class of odd and even to every other post called. So add an extra class of odd or even to the contentcontainer div.

    Here is my code:-

    <div class="maincontent">
        	<h1 class="subpageheader">
    			<?php
                  global $wp_query;
                  $postid = $wp_query->post->ID;
                  echo get_post_meta($postid, 'heading', true);
                ?>
            </h1>
    		<div class="contentcontainer removegap">
    			<?php if (have_posts()) : while (have_posts()) : the_post();?>
                <p><?php the_content(); ?></p>
                <?php endwhile; endif; ?>
                <h2>Q&A</h2>
                <?php
    			$args = array
    			(
    				'numberposts'	=> 5,
    				'category'		=> 4
    			);
    			$posts = get_posts($args);
    			if (count($posts) > 0)
    			{
    				foreach ($posts as $post)
    				{
    					setup_postdata($post);
    			?>
                <div class="questions">
                	<div class="questbox">
                    	<p><?php the_title(); ?></p>
                    </div>
                	<span class="answer"><?php echo get_the_content(); ?></span>
                </div>
                <?php
    				}
    			}
    			?>
            </div>
        </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Not clear if you just wanted the post odd/even, or the whole container. This should give you the idea:

    <?php if (have_posts()) : while (have_posts()) : the_post();?>
       <div class='<?php echo (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?>'>
          <p><?php the_content(); ?></p>
       </div>
    <?php endwhile; endif; ?>

    Thanks, that works great. Just what I needed.

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

The topic ‘style odd and even posts’ is closed to new replies.