• Resolved jrcollins

    (@jrcollins)


    I’m trying to add numbered pagination to a custom category template. The template displays child categories of the current category instead of posts.

    Here are the pagination variables and the code I’m using to get the child categories:

    <?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,
            'posts_per_page' => 3,
            'paged' => $catpage
        )
    );
    ?>

    This displays 3 child categories on the parent category page. Now how do I actually add the pagination links to the page?

Viewing 1 replies (of 1 total)
  • Thread Starter jrcollins

    (@jrcollins)

    I finally managed to get this done. Here’s the code:

    <?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 = 2;
                $offset = ($catnum * $catpage) - 2;
    
            $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
        )
    );
    		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
    				// Start of Pagination
    						$total_childs_query = get_categories( array( 'parent' => $cat_id, 'hide_empty' => '0' ));
    
    						$total_terms = count( $total_childs_query );
    
    						$pages = ceil($total_terms/$catnum);
    
    						$base_url = get_term_link( $cat_id, get_query_var( 'taxonomy' ) );
    
    						// if there's more than one page
    
    						if( $pages > 1 ):
    
    							echo '<div class="ft-paginate">';
    	                        echo '<nav class="navigation pagination" role="navigation">';
    							echo '<h2 class="screen-reader-text">Posts navigation</h2>';
    							echo '<div class="nav-links">';
    
    							// if we're not on the first page, print the previous-link
    
    							if ( $catpage > 1 ) {	
    
    								$prevpage = $catpage - 1;
    
    								if ( $prevpage > 1 ) {	
    
    									echo '<a class="prev page-numbers" href="' . $base_url . '/page/' . $prevpage . '"><i class="fa fa-angle-left"></i></a>';
    								}
    
    								else {
    
    									echo '<a class="prev page-numbers" href="' . $base_url . '"><i class="fa fa-angle-left"></i></a>';
    								}
    
    							}
    
    							for ($pagecount=1; $pagecount <= $pages; $pagecount++):
    
    								//set class
    
    								$class = "page-numbers";
    
    							if ( $pagecount == $catpage ) {
    
    									$class .= " current";	
    
    								}
    
    							if ( $pagecount == $catpage ) {
    
    								echo '&nbsp;<span class="' . $class . '"><span class="screen-reader-text">Page</span>' . $pagecount . '</span>';
    								}
    
    							else if ( $pagecount == 1 ) {
    
    								echo '&nbsp;<a class="' . $class . '" href="' . $base_url . '"><span class="screen-reader-text">Page</span>' . $pagecount . '</a>';
    
    								}
    
    							else {
    
    								echo '&nbsp;<a class="' . $class . '" href="' . $base_url . '/page/' . $pagecount . '"><span class="screen-reader-text">Page</span>' . $pagecount . '</a>';
    
    								}
    
    								endfor;
    
    							// if there is one more page after the current, print the next-link
    
    							if ( $catpage < $pages ) {	
    
    								$nextpage = $catpage + 1;
    
    								echo '&nbsp;<a class="next' . $class . '" href="' . $base_url . '/page/' . $nextpage . '"><i class="fa fa-angle-right"></i></a>';	
    
    							}
    
    							echo '</div>';
    							echo '</nav>';
    
    							printf( '<span class="total-pages">' . esc_html__( 'Page %1$s of %2$s', 'codilight-lite' ) . '</span>', $catpage, $pages );
    
    							echo '</div>';
    
    						endif;
    						// End of Pagination
    						?>
    
    				</main><!-- #main -->
    			</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Pagination for custom category template’ is closed to new replies.