How to show posts with pagination in frontend
-
Hi,
I have connected to the API and using shortcode I can pull the data in frontend. But API have 1000 posts and it shows 100 posts_per_page and total 10 pages.
When i use query parameter I can get the corresponding page. But in frontend I want to show 10 posts_per_page and pagination to show the corresponding page results.
I tried to add query parameter in shortcode and dynamic query to set the page number . But it is not working$current_page = isset($_GET[‘page’]) ? intval($_GET[‘page’]) : 1;
$posts_per_page = 100;
$response = do_shortcode(“[wpgetapi_endpoint api_id=’xxxxxxxxxxx’ endpoint_id=’xxxxx’ page='{$current_page}’ posts_per_page='{$posts_per_page}’ debug=’false’]”);
/ Check if the data contains the pagination details
$total_pages = isset($data[‘total_pages’]) ? $data[‘total_pages’] : 1;/ Display pagination links
if ($total_pages > 1) {
echo ‘// Previous page link
if ($current_page > 1) {
echo ‘<a href=”?page=’ . ($current_page – 1) . ‘”>« Previous</a>’;
}
// Page number links
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $current_page) {
echo ‘<span class=”current-page”>’ . $i . ‘</span>’;
} else {
echo ‘<a href=”?page=’ . $i . ‘”>’ . $i . ‘</a>’;
}
}
// Next page link
if ($current_page < $total_pages) {
echo ‘<a href=”?page=’ . ($current_page + 1) . ‘”>Next »</a>’;
}Please guide me how to get the posts with pagination in the frontend.
Thanks in advance,
- You must be logged in to reply to this topic.