skip to Main Content

I have a WordPress custom page that contains e-commerce items. (https://modeseeker.com/baskets-femme/)

I want to use pagination with parameters like ?page=2. I am aware that WordPress has a pagination system built-in, but I believe I cannot use it as my backend is not wordpress so my items aren’t displayed using wp_query.

The problem is that ‘page’ and ‘p’ are wordpress reserved terms, so when I try to use an url like https://modeseeker.com/baskets-femme/?page=2, it gets redirected to page 1 without param. I tried to disable redirections using add_filter on redirect_canonical() but then I get an error 404.

function page_number_found($redirect_url, $requested_url) {

if (isPageConcatenation($redirect_url, $requested_url)) {
    $redirect_url = $requested_url;
    }
    return $redirect_url;

add_filter('redirect_canonical', 'page_number_found', 10, 2);

I expected this to display page 1 and still have ?page=2 in url, but I get an error 404.

So my question is : is there a way to paginate my page using reserved terms but without using a WordPress query ?

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    I understood why i get an error 404 when using ?page=2 : it depends on the wordpress permalink structure. If the "post name" structure is used, the url looks like /page/2. If I want to use a structure like ?page=2, I have to revert to a "plain" structure, but it doesn't look good. And there is no option in wordpress to choose an hybrid structure like /baskets-femme/?page=2. So basically no solution.


  2. This code verifies the existence of the ‘page’ parameter in the URL and adjusts the ‘paged’ parameter accordingly. Ensure to incorporate this code into either your theme’s functions.php file or a custom plugin.

    function custom_pagination($query) {
        if (is_admin() || !$query->is_main_query()) {
            return;
        }
    
        if (isset($_GET['page']) && is_numeric($_GET['page'])) {
            $query->set('paged', $_GET['page']);
        }
    }
    
    add_action('pre_get_posts', 'custom_pagination');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search