3

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.

1 Answer 1

5

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.

5
  • But that generates domain.com/about/page/1?project=1 Jul 2, 2011 at 20:38
  • "I'm submitting that from a form using get to display the project." Project is a $_GET variable in that link, no? 'add_args' => array( 'project' => absint($_GET['project']) Jul 2, 2011 at 21:18
  • Yes, but again read the main question: 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. Jul 3, 2011 at 2:29
  • I updated my answer, but I'd also like to point out that WordPress already supports that url structure just from using page hierarchy. Jul 3, 2011 at 12:43
  • 1
    Or 'add_args' => array( '#mydiv') if you want the link click to scroll to a div for example. Feb 6, 2020 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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