Search code examples
phpwordpresstemplatescustom-taxonomytaxonomy-terms

How to limit the number of terms in WordPress custom taxonomy terms page


I'm creating theme for movies and TV series. So I have CPT named 'series' and custom Taxonomy named 'shoof_series' and custom terms(categories) for each series. when I created page template to display all terms (series) named (all-series.php) its works fine. but when trying to set how many terms to display ('posts_per_page' = 5) still display all terms in the page.

I have tried this cod.

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $terms = get_terms( 'shoof_series' ); 
    foreach( $terms as $term ) :
        $query = new WP_Query(
           array(
            'posts_per_page' => 1,
            'paged' => $paged,
              'tax_query' => array(
                   array(
                       'taxonomy' => 'shoof_series',
                       'terms' => $term->slug,
                       'field' => 'slug',
                       'hide_empty' => false, // default: true
                   )
              )
           )
        );
        if( $query->have_posts() ):
            while( $query->have_posts() ) : $query->the_post();
              ?>

           <div class="col">
            //my codes 
           </div>

  <?php endwhile;  endif; endforeach; ?> 

<?php wpbeginner_numeric_posts_nav(); ?>

and tried this one too

         <?php
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            $terms = get_terms( array(
              'taxonomy' => 'shoof_series',
              'hide_empty' => false,
            ) );

            if ( empty( $terms ) || is_wp_error( $terms ) ) {
              return;
            }

            foreach( $terms as $term ) {
              ?>

               <div class="col">
                 //my codes 
               </div>

           <?php $term->count;  }?> 

<?php wpbeginner_numeric_posts_nav(); ?>

both of codes work fine, but I cant set how many posts to display per page. so how can I do it?


Solution

  • Since WordPress 4.5+ you need to pass your custom taxonomy in an array… You can use the following arguments in a WP_Term_Query.

    To restrict the number of terms, use the number argument.

    So you can replace in your code:

    $terms = get_terms( 'shoof_series' );
    

    with the following:

    $terms = get_terms( array(
        'taxonomy' => 'product_cat', 
        'number'   => 5, // Here set the desired number of terms
    ) );