Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

last post in a loop

Posted on 2011-10-06
9
Medium Priority
?
213 Views
Last Modified: 2012-08-14

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!
0
Comment
Question by:Refael
  • 5
  • 2
  • 2
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36924033
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36924047
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
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 400 total points
ID: 36924067
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Refael
ID: 36924085
marqusG:

can you tell me how to fix this (your first comment)?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36924086
@hmccurdy: The loop is the second foreach that starts at line 4 of the snippet :-)
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1600 total points
ID: 36924097
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36924104
Ooops: move $postCount = 0; before the loop starts and increment it at the end of the loop! Sorry for confusion...
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36924193
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
 

Author Closing Comment

by:Refael
ID: 37009413
Thank you so much guys!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question