Hồ Danh
Hồ Danh

Reputation: 9

How to paging for WP_Term_Query?

I have a "book-author" taxonomy. I have more than 2000 book-author. I have a book-author.php template to display 2000 book-author. The problem is that I want paging, 10 author per page. Please help me.

$args = array(
'taxonomy'  => 'book-author',
'number'    => 10,
'orderby'   => 'name',
'order'     => 'ASC',
'hide_empty'    => false,

);

$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) {

echo $term->name;
// I want to display 10 book author names here, and the next page will display 10 more book authors.

}

Thanks!

Upvotes: 1

Views: 2298

Answers (1)

danbrellis
danbrellis

Reputation: 399

So to understand, you want to display 10 terms on a page, then on page 2 display the next 10 terms?

For custom pagination like that (Wordpress only offers pagination on posts), you can listen for a custom $_GET variable in your template with a page number and then adjust your term query accordingly.

Here's some quick code to get you on the right path (not tested).

$paged = isset($_GET['term_page']) ? intval($_GET['term_page']) : 1;
if($paged < 1) $paged = 1;

$args = array(
    'taxonomy'  => 'book-author',
    'number'    => 10,
    'offset'    => ($paged - 1) * 10
    'orderby'   => 'name',
    'order'     => 'ASC',
    'hide_empty'    => false,

);

$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) {
    echo $term->name;
}
?>

<a href="add_query_arg('term_page', $paged--)">Prev</a> | <a href="add_query_arg('term_page', $paged++)">Next</a>

Upvotes: 2

Related Questions