DEV Community


Discussion on: How to use the API!

ra1nbow1 profile image
Matvey Romanov

Is it possible to get only 2 latest posts?

nataliedeweerd profile image
𝐍𝐚𝐭𝐚𝐥𝐢𝐞 𝐝𝐞 𝐖𝐞𝐞𝐫𝐝 Author • Edited


Arrays start from 0, meaning if you want the latest 2, you want to stop when the array key (the index of the array item) reaches 1. So you have 0, and 1. The API automatically orders by newest first, so we just need to tell it when to stop!

In the code above, simply change if ($key == 2){ break; } to if ($key == 1){ break; }

Hope that helps :) but let me know if you have anymore questions, or it doesn't make sense.