• Hi, I would like some help with how to change the url that the Pagination block gives to just /page/2. Currently it appears as /?query-8-page=2/.

    I am aware that it cannot be done within the controls of the block so thats why I’m wondering if it could be achieved through a function that I could put in my child theme`s functions.php.

    Thanks in advanced 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    What do you have it set to in settings permalinks?

    What do you have it set to in settings permalinks?

    This was my initial thought as well… but then I fired up a test site to check and found the ?query parameter is used for the query loop navigation irrespective of the permalink setting.

    I’m guessing this is done to avoid conflicting with page break navigation, which uses the /page/2/ to get to the different sections of the single page/post broken into multiple pages.

    Moderator bcworkz

    (@bcworkz)

    Because of what George points out, you don’t want to use /page/2/ format. Choose something else that’s not already in use, for example /read-pg/2/.

    To get WP to recognize such an URL, use add_rewrite_rule() to match requests with /read-pg/, capturing the 2 element of the URL and rewriting it back to index.php??query-8-page=2.
    https://developer.wordpress.org/reference/functions/add_rewrite_rule/

    After adding such a rule, you need to flush the existing rules. One way to do so is by visiting the permalinks settings screen. The other is to call flush_rewrite_rules(). This function should only be called when your theme or plugin that’s adding a rule is activated, using its activation hook. To customize a specific site’s rules, it’s easier to visit permalinks settings.

    Thread Starter ginacc

    (@ginacc)

    I don’t know how to answer indivually, just wanted to thank you all for your time. I have tried to use that function that @bcworkz suggested but I am not very good at writing them functions, all I’ve learnt is by looking up in the internet, copy what I see and hope it works xD,,

    I have used this example:

    add_action( 'init',  function() {
      add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?$', 'index.php?myparamname=$matches[1]', 'top' );
    } );

    And then I went to Admin > Settings > Permalinks > Save and after that I dont know what else to do, sorry. Could you help me with that? Thank you

    Moderator bcworkz

    (@bcworkz)

    myparamname doesn’t mean anything to WP unless you do some other things. I assume query-8-page already has those things. myparamname would not. You also need to use a properly formed URL.

    Isn’t there more to your URL besides query-8-page? I assume it’s for pagination, but pagination of what? That too needs to be captured in your rewrite rule. Your rewrite rule needs to capture all required elements required to get to the right resource. What is the entire URL structure you are after? You can use example.com for domain name, it’s the rest of the URL that matters.

    Thread Starter ginacc

    (@ginacc)

    Hi @bcworkz , so the pagination is for blog posts that I put on the home page using the “Query Loop” block showing a total of 9 posts in a three column grid.

    The URL shows like this when i click on page 2: https://dev.example.com/?query-8-page=2
    When I click on page 3, 4, rest of pages it changes slightly to this: https://dev.example.com/?query-8-page=3&cst

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, it slipped my mind that you were referring to a block’s pagination. I thought we were talking about post pagination for some sort of conventional custom query. One where the template creates a new WP_Query object. TBH I have no idea how the block would paginate posts. In any case, if the query-8-page query var correctly paginates your content, I’d expect a proper rewrite rule to correctly convert your desired URL to the ones you’ve shown examples of in your last reply.

    Something like this should work:

    add_action( 'init',  function() {
      add_rewrite_rule( '^read-pg/([0-9]+)[/]?$', 'index.php?query-8-page=$matches[1]', 'top' );
    } );

    Don’t forget to visit the permalinks settings screen after adding or modifying this code. No need to change or save anything, loading the screen is enough.

    An URL such as example.com/read-pg/3/ should lead to the correct query loop content. However, that’s only half of the puzzle. You also need to alter the code that generates pagination links. How you would do so depends on which function is used to do this.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to change the url of the Pagination block from /?query-8-page=2/ to /page/2/’ is closed to new replies.