Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4108

How to get the API page as a pagination in wordpress frontend

$
0
0

I have connected API that have page, posts_per_page parameter in result body it also have "total": 500, "total_pages": 5, "current_page": 1

The API have default posts_per_page as 100

API page setting:

  $paged = absint($request->get_param('page')) ?: 1;    $posts_per_page = absint($request->get_param('posts_per_page')) ?: 100;     $posts_per_page = min($posts_per_page, 500);//query    $args = array('post_type' => 'posts','posts_per_page' => $posts_per_page,'paged' => $paged,'tax_query' => array('relation' => 'AND',etc............

In Postman if i query the URL as xxx.data?posts_per_page=10&page=2 the results working fine.

I want to show the data in wordpress frontend.when i connected API it shows 100 posts per page . so i plan to give pagination to see other pages and used the below code

// Initialize cURL$curl = curl_init();// Get the current page from the query string (defaults to page 1 if not set)$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;$posts_per_page = isset($_GET['posts_per_page']) ? (int) $_GET['posts_per_page'] : 10; // Default to 10 posts per page// Construct the API URL with dynamic page and posts_per_page parameters$api_url = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdata?' .            http_build_query(['page' => $page,                  // Page parameter for filtering'posts_per_page' => $posts_per_page  // Number of posts per page           ]);// Initialize cURL with API requestcurl_setopt_array($curl, array(  CURLOPT_URL => $api_url,   CURLOPT_RETURNTRANSFER => true,  CURLOPT_ENCODING => '',  CURLOPT_MAXREDIRS => 10,  CURLOPT_TIMEOUT => 0,  CURLOPT_FOLLOWLOCATION => true,  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,  CURLOPT_CUSTOMREQUEST => 'GET',  CURLOPT_HTTPHEADER => array('Authorization: // Replace with your actual token  ),));// Execute cURL and get the response$response = curl_exec($curl);// Close cURL connectioncurl_close($curl);// Decode the response (assumed JSON)$data = json_decode($response, true);// Check if the response contains postsif ($data) {    // Extract pagination info from the response    $total_posts = isset($data['total']) ? $data['total'] : 0;    $total_pages = isset($data['total_pages']) ? $data['total_pages'] : 0;    $current_page = isset($data['current_page']) ? $data['current_page'] : 1;    // Display the posts<?php endif; ?><!-- Pagination Links --><div class="pagination"><!-- Previous Page Link --><?php if ($current_page > 1): ?><a href="<?= add_query_arg(array('page' => $current_page - 1, 'posts_per_page' => $posts_per_page)) ?>">Previous</a><?php endif; ?><!-- Page Number Links --><?php for ($i = 1; $i <= $total_pages; $i++): ?><?php if ($i == $current_page): ?><strong><?= $i ?></strong> <!-- Current page is bold --><?php else: ?><a href="<?= add_query_arg(array('page' => $i, 'posts_per_page' => $posts_per_page)) ?>"><?= $i ?></a><?php endif; ?><?php endfor; ?><!-- Next Page Link --><?php if ($current_page < $total_pages): ?><a href="<?= add_query_arg(array('page' => $current_page + 1, 'posts_per_page' => $posts_per_page)) ?>">Next</a><?php endif; ?></div><?php } else { ?><p>Error fetching data or no data available.</p><?php } 

In frontend i see the 10 posts and the pagination 1 2 3....but when i click the pagination ex: 3 in the URL it only show ?posts_per_page=10 . the page parameter is not accepting and reload the page to same page.

Not sure what the issue tried different function but i cant find any documentation to pull the page in frontend .

As i use wordpress i gave the above code in code snippet and gave the shortcode in one page. Not sure this conflict with the URL changes

Please help me with this issue.

Thanks in advance.


Viewing all articles
Browse latest Browse all 4108