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 '';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __('<<'),
'next_text' => __('>>'),
'current' => max( 1, get_query_var('paged') ),
'total' => $cat
) );
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:
-
Initial Arguments (
$args):
The$argsarray specifies the query parameters for retrieving categories:'parent' => 0ensures that only top-level categories are fetched.'hide_empty' => 0includes empty categories in the results.
-
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 theceil()function to round up. -
Pagination Variables:
$pagedretrieves the current page number from the query variables. Defaults to 1 if not set.$posts_per_pagedefines the number of categories displayed per page (set to 5).$offsetcalculates the starting index for the current page's categories.
-
Updated Arguments for Pagination:
A new$argsarray 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.
-
Fetch and Display Categories:
- The
get_categories()function fetches the categories based on$args. - A
foreachloop iterates through each category and outputs a hyperlink to the category's archive page usingget_category_link()and the category's name.
- The
-
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
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