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!
RefaelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
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 :-)
0
Marco GasiFreelancerCommented:
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);}
0
Hugh McCurdyCommented:
You said it was in a loop but you didn't show the for or while statement.

Try something like this
for ( $postCount = 0; $postCount < 3; $postCount++ )
{
    ....
    echo "<div class='intro_article_container'>";
}

echo "<div class='intro_article_container last'>";

Open in new window


I'm a little concern about using 3 as a constant and we can discuss that further if it bothers you too.  (What if, in the future, the post count should be 5?)

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

RefaelAuthor Commented:
marqusG:

can you tell me how to fix this (your first comment)?
0
Marco GasiFreelancerCommented:
@hmccurdy: The loop is the second foreach that starts at line 4 of the snippet :-)
0
Marco GasiFreelancerCommented:
Move $postCount = 0 at the end of the loop:
$args = array( 'numberposts' => 3, 'order'=> 'DESC', 'orderby' => 'post_date', 'post_status' => 'publish');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
$postCount = 0;
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);

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

Open in new window


This should work ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
Ooops: move $postCount = 0; before the loop starts and increment it at the end of the loop! Sorry for confusion...
0
Hugh McCurdyCommented:
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.

0
RefaelAuthor Commented:
Thank you so much guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.