Change The Last Post In The Loop In WordPress

/ Wordpress / by Paul Robinson / 10 Comments
This post was published back on July 22, 2011 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Changing the last post displayed in the loop isn’t really that difficult if you have knowledge of the variables available inside $wp_query. However changing the last post, only if it is truly the last post, is a lot more difficult.

What Do You Mean? Truly The Last Post?

A lot of tutorials show you how to change the last post displayed on the current page, meaning that on each page (if your loop is paginated) your last post will be different. What I was asked though was just to change the last post, by that I mean the last post on the last page. For example if a query returns 30 posts I only want to change the 30th post, not the 10th, 20th & 30th (assuming 10 posts are displayed per page).

Ahh. How Do You Do It Then?

Well first, let’s take a look at what you might use it for.

Change Last Loop Item WordPress

As you can see, it has been used to promote the ability to visit another website where more products are available. Thanks to Lisa Marie for the image.

So how is it done I hear you cry. Well it’s done using some variables held inside $wp_query and some fairly simple Math. Let’s take a look at the code.

Okay. I know it looks confusing, but let’s break this down. The sum is bracketed to control how the computer completes the sum, so let’s do it in order.

First we take the current page number ($wp_query->query_vars['paged']) minus one to give us the number of the previous page, we then multiply that by the number of posts per page ($wp_query->query_vars['posts_per_page']).

Then we take the current post number ($wp_query->current_post) and add one to it.

Now we take the result from the first set of sums (paragraph 1) & the second set of sums (paragraph 2) and add them together. Then check if that is equal to the number found in $wp_query->found_posts which contains the number of posts found for the query in total.

Is That All?

Yes, believe it or not, that’s it. If you want to change the last post on each page the code would instead be:

That code is available all over the place, but I thought I’d put it here for reference sake.

I hope this tutorial has helped you in some way. If you have any questions about anything in this post please leave a comment below & I’ll answer as soon as I can. Also please don’t forget you can follow me on Twitter or if you want to help us out you can donate using the link in the box below this post.

10 Comments

Author’s gravatar

You know, math is hard enough when you are just doing it all by itself. Throw it into programming and you start down a road of all kinds of new headaches. I never have like doing math in programming. This kind of thing has always confused me. Nice tutorial, I can see the many different uses this kind of thing would have.

Reply
Author’s gravatar author

Thanks Jamie.

I hate Math. I understand it is a necessary part of everyday life, but I have always found it difficult. That’s why I thought it was really important to explain how the Math behind this tutorial worked in an easy way.

I’m glad you liked the tutorial, I wasn’t sure if anyone would be able to find a use for it. So consider me super happy that you can think some good uses for it. 🙂

Author’s gravatar

This is an awesome tips !! Thanks.

Although i could implement it when i can get my custom post types to paginate properly. LOL, from the second page and on, i always get a 404. Being playing with this for a while, to no avail.

Reply
Author’s gravatar author

The problem with Custom Post Types not paginating is a common one.

There are a few different causes the most common one is the posts per page setting going a bit strange. I don’t know why this happens but I’ve come across it a lot.

In your admin under ‘settings -> reading’ there is a setting ‘Blog pages show at most’. There is a strange bug where if you are trying to display less posts than the amount set in that setting you will get a 404. It’s something to do with how the query is generated.

The only way I’ve found around it is to either display an amount of posts equal to or higher than the ‘Blog pages show at most’ setting. If you want to display less you will have to turn that setting down. As long as you don’t try to show less posts than that setting you shouldn’t have any problems with 404’s once you have resaved your permalinks page.

That is the problem I most often come across. I hope it helps fix your problem. 😉

Also thanks, glad you found the post helpful. 🙂

Author’s gravatar

Thanks! You just ended a day of frustration and weak workarounds.

Reply
Author’s gravatar author

Hi Mike,

No problem. Glad I was able to help.

Author’s gravatar

Could I use a simular technique for my problem?

I need to list post_title’s in a specific format with the last title in the list to have a slight variation.

Here’s what I need to format (note no camma after last title)

“post_title”,”post_title”,”post_title”

I thought a mini-loop might work. Here’s what I’m using

”,
]”>

But I need the final post_title to have no trailing comma?

Reply
Author’s gravatar

** Take 2 on the code paste

Reply
Author’s gravatar author

Hi,

The best way to do that might be this instead:

That should produce the result you are after with a little less fuss. 😉

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more