Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

last post in a loop


Hello Experts,

In my code below I am trying to determine if this is the last post, (post number 3).
If yes then I need to echo a different style to the container div

Here is what I am trying to do in the loop but it does not seem to work:

 
$args = array( 'numberposts' => 3, 'order'=> 'DESC', 'orderby' => 'post_date', 'post_status' => 'publish');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
foreach((get_the_category()) as $category) {
$category_link = get_category_link($category);
$permalink = get_permalink($id);
$postSubTitle = get_post_meta($post->ID, ' _ex_mbst_name', true);



$postCount = 0;
if ($postCount == sizeof($postslist)) {
       echo "<div class='intro_article_container last'>";
} else {
	echo "<div class='intro_article_container'>";
}

Open in new window


I would appreciate your help!
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

You are checking if $postCount is equal to the lengfth of the array immediately after having assigned to the $postCount the value 0 so it will be never equal to the array length :-)
Another thing:

foreach ($postslist as $post) : setup_postdata($post);

Perhaps I don't know this syntax and it's correct, but I would write

foreach ($postslist as $post) { setup_postdata($post);}
SOLUTION
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael

ASKER

marqusG:

can you tell me how to fix this (your first comment)?
@hmccurdy: The loop is the second foreach that starts at line 4 of the snippet :-)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops: move $postCount = 0; before the loop starts and increment it at the end of the loop! Sorry for confusion...
I see that now marqusG.  I didn't see a closing } so I didn't look for an opening {
I see you added a closing }

It looks like your code will work because you have initialized $postCount before the loop and are incrementing it at the bottom of the loop.

Avatar of Refael

ASKER

Thank you so much guys!