Passing custom args in paginate_links

For example i’ve domain.com/about?project=1 but i’ve permalinks enabled and i’m using thins to generate the pagination paginate_links. The proble is that the links generated are like this domain.com/about?project=1/page/1 and it breaks everything because the project is taken as 1/page/1

There is a way to get domain.com/about/project/1/page/1 in the paginations links? I’m submitting that from a form using get to display the project.

Related posts

Leave a Reply

1 comment

  1. To add arguments to the links in paginate posts, use the 'add_args' argument in the function. You have to pass the arguments as an associative array. So, to add project=1 to the end of all your links, you would do this:

    global $wp_query;
    paginate_links(array(
      'total' => $wp_query->max_num_pages,
      'current' => (get_query_var('paged') ? get_query_var('paged') : 1),
      'base' => 'http://domain.com/about/%_%',
      'format' => 'page/%#%',
      'add_args' => array( 'project' => 1 /* or whatever the project number is*/ ),
    ));
    

    Hope that helped!

    EDIT

    To get domain.com/about/projects/1/page/1, you can add a custom permastruct. I’m going to assume about is a page.

    function wpse21802_init(){
      add_rewrite_rule( '([^/]+)/projects/([^/])/?$', 'index.php?pagename=$matches[1]&project=$matches[2]', 'top' );
      add_rewrite_rule( '([^/]+)/projects/([^/])/page/(/d+)/?$', 'index.php?pagename=$matches[1]&project=$matches[2]&paged=$matches[3]', 'top' );
    }
    add_action( 'init', 'wpse21802_init' );
    

    After adding that code, flush the rewrite rules by going to Settings -> Permalinks.