How to paginate a list of tags

I have a page that displays a list of all tags of my blog. Iuse this code which works fine:

<?php
$poststocount=get_tags($args);
echo '<h2>Alphabetic Index of All '</h2>';
?>

<?php
$taxonomy = 'post_tag';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>

Question: how can I paginate this lidt (say 40 per page). What would the code to accompish that, preferably with an option to “see all tags”.

Read More

Thanks.

Related posts

Leave a Reply

2 comments

  1. you could paginate your page by simply adding /page/n to the end of the URL, where n is the desired page number. creating your next/prev links will be a manual affair though. the page number will then be accessible via get_query_var('paged'). then use the number argument for get_terms to select 40 at a time, use the offset argument, which will be your page number -1 * number per page, to select the current “page” of terms:

    $page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    $taxonomy = 'post_tag';
    $offset = ( $page-1 ) * 40;
    $args = array( 'number' => 40, 'offset' => $offset );
    $tax_terms = get_terms( $taxonomy, $args );
    

    as for viewing all, maybe append a GET var to the URL, ?showall=true, then check isset( $_GET['showall'] ) and change the number to fetch accordingly.

    EDIT

    here’s a quick template I made to show an example. I tested it on a page on a test install with pretty permalinks, the page was ‘about’, so the pagination links were:

    http://localhost/about/page/2/
    http://localhost/about/?showall=true
    

    if your permalinks are different, you’ll have to edit the pagination section to reflect your setup.

    <?php
    
    get_header();
    
    // if show all is set
    if( isset($_GET['showall']) ):
    
        $args = array( 'hide_empty' => 0 );
    
    else:
    // else show paged
    
        $page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
        // number of tags to show per-page
        $per_page = 40;
        $offset = ( $page-1 ) * $per_page;
        $args = array( 'number' => $per_page, 'offset' => $offset, 'hide_empty' => 0 );
    
    endif;
    
    $taxonomy = 'post_tag';
    $tax_terms = get_terms( $taxonomy, $args );
    
    
    echo '<ul>';
    
    foreach ($tax_terms as $tax_term) {
        echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
    }
    
    echo '</ul>';
    
    
    // pagination
    // if showall isn't set
    if( !isset($_GET['showall']) ):
    
        $total_terms = wp_count_terms( 'post_tag' );
        $pages = ceil($total_terms/$per_page);
    
        // if there's more than one page
        if( $pages > 1 ):
            echo '<ul>';
    
            for ($pagecount=1; $pagecount <= $pages; $pagecount++):
                echo '<li><a href="'.get_permalink().'page/'.$pagecount.'/">'.$pagecount.'</a></li>';
            endfor;
    
            echo '</ul>';
    
            // link to show all
            echo '<a href="'.get_permalink().'?showall=true">show all</a>';
        endif;
    
    else:
    // showall is set, show link to get back to paged mode
    
        echo '<a href="'.get_permalink().'">show paged</a>';
    
    endif;
    
    
    get_footer();
    
    ?>
    
  2. This question asked long time ago. Here is the easiest solution I found.

    $page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    $per_page = 50;
    $offset = ( $page-1 ) * $per_page;
    $total_terms = wp_count_terms('cities');
    $all_cities = get_terms( array(
        'taxonomy'      => 'cities',
        'offset'        => $offset,
        'number'        => $per_page
    ) );
    

    Then use paginate_links() function after loop.

    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => $page,
        'total' => ceil($total_terms / $per_page)
    ));