PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How to paging for WP_Term_Query?

 August 01, 2022     pagination, php, wordpress     No comments   

Issue

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!


Solution

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>


Answered By - danbrellis
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing