Custom category template pagination problem

Well, modifying this codilight_lite_custom_paginate() function is not a good option. Cause if it gets modified then it might cause problem on other pages. So if you want to paginate your archive.php child category foreach loop then remove the codilight_lite_custom_paginate() function and customize your archive.php page like below –

<?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
            $cat = get_category( get_query_var( 'cat' ) );
            $cat_id = $cat->cat_ID;
            $child_categories=get_categories(
                array(
                    'parent' => $cat_id,
                    // Uncomment the below line if you want empty category to appear on the list.
                    'hide_empty'   => 0
                )
            );

            // Variables for pagination
            $page = isset($_GET['page'])?intval($_GET['page']-1):0;
            $per_page_cat_num = 10;
            $number_of_pages = intval(count($child_categories)/$per_page_cat_num)+1;

            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 ( array_slice($child_categories, $page*$per_page_cat_num, $per_page_cat_num) as $child ){
                    $count++;
                    include( locate_template( 'loop-templates/content-grid.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; ?>

            <!-- Pagination HTML starts here. Style it how ever you want -->
            <ul id='paginator'>
                <?php
                for( $i=1; $i<$number_of_pages; $i++){?>
                    <li><a href="https://wordpress.stackexchange.com/questions/236899/./?page=<?php echo $i ?>"><?php echo $i ?></a></li>
                <?php } ?>
            </ul>
            <!-- Pagination HTML ends here -->

        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Here $per_page_cat_num holds the number of child category you want to show per page. I’ve assign it default to 10. Change it as your context.

Leave a Comment