[COS Blog Post API] How to implement paging to blog listing


#1

Hi,

API documentation indicates that blog listing supports pagination. Is there any starting point, tutorial or advice how to implement pagination?
Basically what i want to achieve, is that page lists about ten newest blog post and older posts can be loaded via prev (next) buttons.
I’m working on PHP & Javascript. (obviously i don’t have strong coding background) :slight_smile:


#2

Hi, wintertribe,

Not sure, if you still need help but that post might help someone else.

So, if you look closer the HubSpot API's following endpoint https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts, you will see that there are 2 query-filter parameters, "limit" & "offset" and then you also have the "total_count".
With these 3 variables, you can determine which part of the total blog-list you want to get from HS database.

Then, what you will need is a variable "page" that will indicate in which page the user currently is.

So let's take a step closer to a potential scenario:

scenario requirements
a) show me the 1st page of my blog, b) each page should contain 8 blog-items

To implement that scenario you could (for example in PHP):

  1. Add "page" URL parameters in your page (PHP):
    index.php?page=1

  2. Calculate your variables (PHP):
    $limit = 8;
    $offset = (($page - 1) * $limit) - 1;
    $offset = ($offset < 0) ? 0 : $offset;

So, in case of page 1, the values will be:
$page -> 1
$limit -> 8
$offset -> 0

In case of page 2, the values will be:
$page -> 2
$limit -> 8
$offset -> 7

In case of page 3, the values will be:
$page -> 3
$limit -> 8
$offset -> 15

etc.

  1. Perform the HubSpot-API request:
    The string endpoint to get posts should be something like ->
    'https://api.hubapi.com/content/api/v2/blog-posts/?hapikey='.$apikey.'&limit='.$limit.'&offfset='.$offfset;
    For example, the string endpoint to get the first 8 posts should be something like ->
    'https://api.hubapi.com/content/api/v2/blog-posts/?hapikey=blablalba&limit=8&offset=0;

  2. Add pagination buttons "prev" & "next":
    Having all the needed variables at that point, you should be able to show:

  • Prev button if the current page is not the first.
  • Next button if the current page is not the last.

I hope that was helpful.