0

So I've been looking for a solution to this for a while to no avail. There's a lot of information out there about creating a different output for even numbered posts vs odd numbered posts, but I need something differently.

Basically I have dynamically loaded posts added to a page. They are all 50% of the container with a float: left CSS rule. So if the total number of posts is even it looks great, if it's odd, there's an awkward empty space after the last post loaded to the page.

I'd like to create a rule that states if total number of post is even, apply this mark-up, if is odd, apply alternate mark up. Then I can take the last child of mark up and force the width to 100% instead of 50%.

Here's my loop:

<?php $the_query = new WP_Query();
    $x = 0;
    while ( $the_query->have_posts() ) :
        $the_query->the_post(); ?>

        <article>
            Some Content
        </article>
    <?php endwhile;
    $x++;
wp_reset_query(); ?>

CSS:

article { width: 50%; float: left; }

What I want is:

<?php if ($post_count = even) : ?>
    <article>
        Some Content
    </article>
<?php elseif ($post_count = odd) : ?>
    <article class="alt">
        Some Content
    </article>
<?php endif; ?>

CSS:

article { width: 50%; float: left; }
article.alt:last-child { width: 100%; }

Is anyone familiar with how this can be achieved? As always, any help is greatly appreciated!

cheers,

Eric Brockman
  • 824
  • 2
  • 10
  • 37

2 Answers2

1

the modulus (%) operator can be used here

if (($x % 2) == 1)
{
  echo "odd";
}

if (($x % 2) == 0)
{
  echo "even";
}

See: php test if number is odd or even

Community
  • 1
  • 1
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • For some reason the code is outputting the `else` or `if (($x % 2) == 1)` markup regardless if total number of posts is even or odd. Just to clarify, and apologies for being such a novice, but am I putting this code between while loop in the above example? – Eric Brockman Nov 03 '15 at 04:14
0

You can try this :

$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
if($published_posts % 2 == 0) 
{
    // even
}
else
{
    //odd
}

If you want to know whether respective post is even or odd...please refer to below :

<?php $postnum = 0;?>
    if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php $postnum++;
    $alt = ( $postnum % 2 ) ? ' even_post' : ' odd_post';
?>
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
  • For some reason the code is outputting the `else` markup regardless if total number of posts is even or odd. Just to clarify, and apologies for being such a novice, but am I putting this code between while loop in the above example? – Eric Brockman Nov 03 '15 at 04:11
  • if you want to identify the even and odd post inside while loop..then go for `$alt = ( $postnum % 2 ) ? ' even_post' : ' odd_post';` otherwise you can directly `$count_posts = wp_count_posts();` count the post and do the needful.. – Happy Coding Nov 03 '15 at 04:14
  • I don't want to identify if the post is evenly numbered or odd and apply different markup for each, I want to find the total number of posts and apply different mark up if the total number is evenly numbered or odd. Unfortunately, I've added your suggestion right between the while loop in my code and the output stays the same regardless if there's (say) 2 posts or 3 posts. – Eric Brockman Nov 03 '15 at 04:17
  • I've modified the code..please try it and let me know whether it's working – Happy Coding Nov 03 '15 at 04:24
  • Hmmm, still getting the same results. Regardless if there are (say) 2 posts or 3 post, the code outputs only the `else` version of the output. To confirm this is supposed to go in between when the while loop is opened and closed, correct? – Eric Brockman Nov 03 '15 at 04:43
  • Be happy to, how is the safest way to do that? I can't add whole blocks of code as a comment. Is there a private message option in the sf forum? – Eric Brockman Nov 03 '15 at 04:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94050/discussion-between-raveenanigam-and-eric-brockman). – Happy Coding Nov 03 '15 at 04:49
  • Your help was exactly on point, so long as the code you added is outside of the while statement. Thanks! – Eric Brockman Nov 03 '15 at 05:17