WordPress: How to Add Pagination to a List of Categories

Question: How can we integrate pagination with a list of categories in WordPress?

Answer: Below is the code to integrate pagination with a list of categories in WordPress.

 
$args = array(
	'parent' => 0,
	'hide_empty' => 0
);

$categories = get_categories( $args );
$cat =  ceil(count( $categories )/5);    
$j=0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 5;
$offset = ($posts_per_page * $paged) - 5 ;
$args = array(
	'orderby' => 'name',
	'parent' => 0,
	'hide_empty'         => 0,
	'number' => $posts_per_page,
	'offset' => $offset,
	'posts_per_page' => 5,
	'paged' => $paged
	//'exclude'            => '1,3,24,9'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
 $j++;
 echo '' . $category->name . '
'; } $big = 999999999; // need an unlikely integer echo '';

Here’s an explanation of the provided code:

This PHP script integrates pagination with a list of WordPress categories, enabling you to display a limited number of categories per page. Here's a breakdown of the code:

  1. Initial Arguments ($args):
    The $args array specifies the query parameters for retrieving categories:

    • 'parent' => 0 ensures that only top-level categories are fetched.
    • 'hide_empty' => 0 includes empty categories in the results.
  2. Total Pages Calculation ($cat):
    The total number of pages is calculated by dividing the total count of categories by the number of categories per page (5 in this case), using the ceil() function to round up.

  3. Pagination Variables:

    • $paged retrieves the current page number from the query variables. Defaults to 1 if not set.
    • $posts_per_page defines the number of categories displayed per page (set to 5).
    • $offset calculates the starting index for the current page's categories.
  4. Updated Arguments for Pagination:
    A new $args array is created for fetching paginated categories:

    • 'number' and 'posts_per_page' limit the results to 5 per page.
    • 'offset' determines where the results start for the current page.
  5. Fetch and Display Categories:

    • The get_categories() function fetches the categories based on $args.
    • A foreach loop iterates through each category and outputs a hyperlink to the category's archive page using get_category_link() and the category's name.
  6. Pagination Links:

    • The paginate_links() function generates the pagination controls.
    • The 'base' parameter sets the URL structure, with %#% replaced by the page number.
    • 'format' appends the page number as a query string (?paged=%#%).
    • 'prev_text' and 'next_text' define custom labels for the "previous" and "next" buttons.
    • 'current' indicates the current page, and 'total' specifies the total number of pages ($cat).

The result is a paginated list of top-level categories displayed as links, with navigation controls to move between pages.

Let me know if you need further clarification!

No comments:

Post a Comment