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!