0

I'm using this code to try and check the last post on each page loop is even so i can add different classes.

This works to check if its the last post :

( ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count )

This works to check if its a even post

( $wp_query->current_post % 2 == 0 )

But this doesn't check if its the last post and even.

 ( ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count ) && ( $wp_query->current_post % 2 == 0 );

The problem i have is i want to display posts in columns and avoid the last post displaying in a column with a gap when there's a even amount of posts.

0
<?php if( have_posts() ) : ?>

    <?php $count = 0; ?>

    <?php while( have_posts() ) : the_post(); ?>

    <?php
    global $count;

    $col = 'col-lg-6';

    if($count%3 == 0) {
        $col = 'col-lg-12';
    } else {
        $col = 'col-lg-6';
    }

    $count++;

    ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class( $col ); ?>>
        [...]
        [...]
        [...]
    </article>

    <?php endwhile; ?>
<?php endif; ?>
| improve this answer | |
0

In general I think the column layout should be handled with CSS, without the need to change the generated HTML. The number of columns might need to change with device, etc.

So here I just want to look at this statement:

This works to check if its the last post :
( ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count )

This looks rather complex and would most likely slow down developers trying to evaluate this expression.

Let's drill into this:

The $wp_query->current_post has -1 as initial value, that's incremented for each post in the loop.

The following source references are related to that:

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-wp-query.php#L488

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-wp-query.php#L3069

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-wp-query.php#L3085

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-wp-query.php#L3115

The first post in the loop has $wp_query->current_post as 0. Then this statement becomes:

   ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count
=> ( 1 == 0 + 1 ) == $wp_query->post_count
=> ( 1 == 1 ) == $wp_query->post_count
=> true == $wp_query->post_count

There are parentheses missing, so we need to check the operator precedence:

https://secure.php.net/manual/en/language.operators.precedence.php

Hopefully I have this correct :-)

The second post in the loop has $wp_query->current_post as 1. Then the statement becomes:

   ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count
=> ( 1 == 1 + 1 ) == $wp_query->post_count
=> ( 1 == 2 ) == $wp_query->post_count
=> false == $wp_query->post_count

etc

So the check:

( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count

seems to boil down to either:

true == $wp_query->post_count

for the first post in the loop or else

false == $wp_query->post_count

which is probably not what you have in mind.

To check for the last post in the loop, it seems your'e looking for:

current_post(s) | post_count 
----------------------------
 -1                   0
 0*                   1
 0, 1*                2
 0, 1, 2*             3
 0, 1, 2, 3*          4
...

where * marks the last post index in the loop.

The rule to determine the last post in the loop (*), using the current post index and the post count, seems to be:

$wp_query->current_post + 1 === $wp_query->post_count

where $wp_query->post_count > 0.

Also check out the same loop ending condition here:

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-wp-query.php#L3115

Hope this helps you to investigate this further!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.