How to create paginated navigation links in WordPress with paginate_links

The WordPress paginate_links function is used to generate a paginated navigation list for splitting long content into multiple pages. It can be useful for improving user experience by breaking up lengthy content into more manageable chunks, making it easier for readers to navigate through the content.

The function can be customized to display the pagination links in different styles and formats, allowing developers to tailor the navigation to fit the design and layout of their WordPress site.

  • It provides a convenient way to add pagination to custom queries, such as custom loops or custom post types, without having to manually write the pagination logic.
  • It also supports various pagination styles, including numbered links, next/previous links, and more, giving developers flexibility in how they want to present the pagination to their users.

The paginate_links function is a valuable tool for enhancing the usability of WordPress websites by facilitating the navigation of long content through paginated links.

Parameters Accepted by WordPress paginate_links Function

The paginate_links function in WordPress accepts the following parameters:

  • $args (string|array, optional. Default value: ”): Array or string of arguments for generating paginated links for archives.

Value Returned by WordPress paginate_links Function

The paginate_links function in WordPress returns the following:

string|string[]|void: String of page links or array of page links, depending on ‘type’ argument. Void if total number of pages is less than 2.

Examples

How to create pagination links for a custom query

$page = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
 'post_type' => 'custom_post_type',
 'posts_per_page' => 10,
 'paged' => $page
);
$custom_query = new WP_Query($args);

// ... Loop through the custom query results ...

echo paginate_links(array(
 'total' => $custom_query->max_num_pages
));

This code snippet creates pagination links for a custom query by using the paginate_links function. It first checks the current page number using get_query_var, then sets up the custom query with the desired post type and posts per page. After looping through the custom query results, it outputs the pagination links using paginate_links with the total number of pages from the custom query.

How to customize the pagination links output

echo paginate_links(array(
 'base' => '%_%',
 'format' => '?page=%#%',
 'total' => 10,
 'current' => 3,
 'show_all' => false,
 'end_size' => 1,
 'mid_size' => 2,
 'prev_next' => true,
 'prev_text' => 'Previous',
 'next_text' => 'Next',
 'type' => 'list'
));

This code snippet demonstrates customizing the output of pagination links using various parameters in the paginate_links function. It sets the base and format for the links, total number of pages, current page number, and options for showing all, end size, mid size, previous and next link text, and the type of output (in this case, a list).

How to add pagination links to a custom post type archive

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
 'post_type' => 'custom_post_type',
 'posts_per_page' => 5,
 'paged' => $paged
);
$custom_query = new WP_Query( $args );

// ... Loop through the custom query results ...

echo paginate_links( array(
 'total' => $custom_query->max_num_pages
) );

This code snippet adds pagination links to a custom post type archive by setting up a custom query for the post type with a specified number of posts per page. After looping through the custom query results, it outputs the pagination links using paginate_links with the total number of pages from the custom query.

Conclusion

In conclusion, the paginate_links function is a powerful tool for adding pagination to WordPress websites. With its flexible parameters and easy integration, it allows developers to create customizable and user-friendly pagination for their content. By using this function, website owners can improve the navigation experience for their visitors and enhance the overall usability of their site. Whether you’re a beginner or an experienced developer, paginate_links is a valuable addition to your toolkit for managing and displaying paginated content.

Related WordPress Functions