• Resolved jrcollins

    (@jrcollins)


    I’m trying to generate numbered pagination links on a custom category template. The template displays the current category’s child categories. I’ve pasted the code I’m using below. At the moment there are no links showing and I haven’t been able to work out what’s wrong.

    <?php
    
    $total_terms = count( $child_categories );
    
    $pages = ceil($total_terms/$catnum);
    
    // if there's more than one page
    
    if( $pages > 1 ):
    
        echo '<div class="ft-paginate"><ul class="paginate">';
    
        // if we're not on the first page, print the previous-link
    
        if ( $catpage > 1 ) {   
    
            $prevpage = $catpage - 1;
    
            if ( $prevpage > 1 ) {  
    
                echo '<li class="previous-page"><a href="'. get_permalink() .'?paged='. $prevpage .'/"><i class="fa fa-angle-left"></i></a></li>';
            }
    
            else {
    
                echo '<li class="previous-page"><a href="'. get_permalink() .'"> &laquo; </a></li>';    
    
            }
    
        }
    
        for ($pagecount=1; $pagecount <= $pages; $pagecount++):
    
            //set class
    
            $class = "page-num";
    
            if ( $pagecount == $catpage ) {
    
                $class .= " current-page";  
    
            }
    
            // print number
    
            echo '<li class="'. $class .'"><a href="'. get_permalink() .'?paged='. $pagecount .'/">'. $pagecount. '</a></li>';
    
        endfor;
    
        // if there is one more page after the current, print the next-link
    
        if ( $catpage < $pages ) {  
    
            $nextpage = $catpage + 1;
    
            echo '<li class="previous-page"><a href="'. get_permalink() .'?paged='. $nextpage .'/"><i class="fa fa-angle-right"></i></a></li>'; 
    
        }
    
        echo '</ul>';
    
        printf( '<span class="total-pages">'. esc_html__( 'Page %1$s of %2$s', 'codilight-lite' ) .'</span>', $catpage, $pages );
    
        echo '</div>';
    
    endif;
    
    ?>

    Here is my custom category template with the above pagination code included.

    <?php
    /**
     * Category Template: Custom
     */
    
    get_header(); ?>
        <div id="content" class="site-content container <?php echo codilight_lite_sidebar_position(); ?>">
            <div class="content-inside">
                <div id="primary" class="content-area">
                    <main id="main" class="site-main" role="main">
    <?php
            $catpage = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                $catnum = 3;
                $offset = ($catnum * $catpage) - 3;
    
            $cat = get_category( get_query_var( 'cat' ) );
            $cat_id = $cat->cat_ID;
            $child_categories=get_categories(
            array(
            'parent' => $cat_id,
            'orderby' => 'id',
            'order' => 'DESC',
            'hide_empty' => '0',
            'number' => $catnum,
            'offset' => $offset,
            'paged' => $catpage
            // Uncomment the below line if you want empty category to appear on the list.
            // 'hide_empty'   => 0
        )
    );
            if (!empty($child_categories)) : $count = 0; ?>
    
                        <header class="page-header">
                            <?php
                                the_archive_title( '<h1 class="page-title">', '</h1>' );
                                the_archive_description( '<div class="taxonomy-description">', '</div>' );
                            ?>
                        </header><!-- .page-header -->
    
                        <?php
                            echo '<div class="block1 block1_grid">';
                            echo '<div class="row">';
    
                        foreach ( $child_categories as $child ){ $count++;
                        include( locate_template( 'template-parts/content-custom.php' ) );
    
                        if ( $count % 2 == 0 ) {
                                    echo '</div>';
                                    echo '<div class="row">';
                        }
                        }
                            echo '</div>';
                            echo '</div>';
                        ?>
    
                    <?php else : ?>
    
                        <?php get_template_part( 'template-parts/content', 'none' ); ?>
    
                    <?php endif; ?>
    
                    <?php
    
    $total_terms = count( $child_categories );
    
    $pages = ceil($total_terms/$catnum);
    
    // if there's more than one page
    
    if( $pages > 1 ):
    
        echo '<div class="ft-paginate"><ul class="paginate">';
    
        // if we're not on the first page, print the previous-link
    
        if ( $catpage > 1 ) {   
    
            $prevpage = $catpage - 1;
    
            if ( $prevpage > 1 ) {  
    
                echo '<li class="previous-page"><a href="'. get_permalink() .'?paged='. $prevpage .'/"><i class="fa fa-angle-left"></i></a></li>';  
    
            }
    
            else {
    
                echo '<li class="previous-page"><a href="'. get_permalink() .'"> &laquo; </a></li>';    
    
            }
    
        }
    
        for ($pagecount=1; $pagecount <= $pages; $pagecount++):
    
            //set class
    
            $class = "page-num";
    
            if ( $pagecount == $catpage ) {
    
                $class .= " current-page";  
    
            }
    
            // print number
    
            echo '<li class="'. $class .'"><a href="'. get_permalink() .'?paged='. $pagecount .'/">'. $pagecount. '</a></li>';
    
        endfor;
    
        // if there is one more page after the current, print the next-link
    
        if ( $catpage < $pages ) {  
    
            $nextpage = $catpage + 1;
    
            echo '<li class="previous-page"><a href="'. get_permalink() .'?paged='. $nextpage .'/"><i class="fa fa-angle-right"></i></a></li>'; 
    
        }
    
        echo '</ul>';
    
        printf( '<span class="total-pages">'. esc_html__( 'Page %1$s of %2$s', 'codilight-lite' ) .'</span>', $catpage, $pages );
    
        echo '</div>';
    
    endif;
    
    ?>
    
                    </main><!-- #main -->
                </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hello!

    I think you may be using the wrong functions. Have you looked over the Pagination codex page ( https://codex.wordpress.org/Pagination )

    What happens if you try using paginate_links() instead?

    codex page for it:
    https://codex.wordpress.org/Function_Reference/paginate_links

    developer handbook:
    https://developer.wordpress.org/reference/functions/paginate_links/

    Let us know if that helps!

    Thread Starter jrcollins

    (@jrcollins)

    Hi, thank you for the suggestions. I seem to have it working now. There was a mistake made with the code to count the child categories. Instead of getting the total number of categories it was only getting the categories on that page after they had been filtered by the get_categories() array.

    I replaced the first few lines with the following:

    $total_childs_query = get_categories( array( 'parent' => $cat_id, 'hide_empty' => '0' ));
    
    						$total_terms = count( $total_childs_query );
    
    						$pages = ceil($total_terms/$catnum);

    The navigation links are for a custom category template listing categories, not posts so I’m not able to use the standard template tags.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Unable to display pagination links’ is closed to new replies.