0

Hi I am trying to run a pagination rule for the following for some custom pages that are getting data from an external api

the current structure is like and it working fine

http://domain.com/?page_id=55&page=2

but want it to be like

http://domain.com/pagename/2

I have tried to set the following permalinks but it it not working

/%postname%/?page=$


/page_id=%post_id%&page=$

Can you please guide how to get it to work?

  • 1
    Just set your permalinks to /%postname%/, pagination rewrite rules are automatically added and http://domain.com/pagename/2/ will work. – Milo Nov 26 '16 at 16:44
0

If the data is coming from a third-party, then putting rules into the Permalinks section of the WP admin panel won't work.

You'll need to write some code to register your own URL parameter(s) for the pagination, in order to achieve clean URLs.

First, you need to register the parameter, I suggest not calling it page as this may conflict with WP's native ones.

function custom_query_vars_filter($vars) {
  $vars[] = 'pg';
  return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );

After adding your custom query vars, you need to flush the permalinks for WP to pick up on your new custom query vars - to do this, simply resave the permalink structure in the admin panel settings.

Then, wherever you are outputting a link to a URL that includes your custom paging URL parameter, do this to create the link:

add_query_arg(array('pg' => 2), '/pagename');

And then on the page, to get the page number to use in the query to the API, do this:

get_query_var('pg');

Also, you need to set permalink structure to post name (in the admin panel settings) if you haven't already.

| improve this answer | |
  • Also, forgot to say, you need to flush the permalinks for WP to pick up on your new custom query vars - to do this, simply resave the permalink structure in the admin panel settings. – Dan. Nov 26 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.