1

I have customized my theme's category template to display the current category's child categories instead of posts. Everything is working correctly except for the following error: Fatal error: Call to undefined function codilight_lite_custom_paginate() in...

I could remove the function from the template but if possible I would like to keep it so I have the option to paginate the results for categories with a large number of child categories.

Is there any way I can modify the function so it works with the customized template?

Here is the function:

if ( ! function_exists( 'codilight_lite_custom_paginate' ) ) :
/**
 * Retrieve paginated link for archive post pages.
 */
function codilight_lite_custom_paginate() {
    global $wp_query;
    $total_pages = $wp_query->max_num_pages;
    $big         = 999999999;
    $translated  = __( 'Page', 'codilight-lite' );

    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));
        echo '<div class="ft-paginate">';
        echo paginate_links(array(
            'base'               => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'             => '?paged=%#%',
            'prev_next'          => True,
            'prev_text'          => '<i class="fa fa-angle-left"></i>',
            'next_text'          => '<i class="fa fa-angle-right"></i>',
            'current'            => $current_page,
            'total'              => $total_pages,
            'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
        ));
        //echo '<span class="total-pages">Page '. $current_page .' of '. $total_pages .'</span>';
        printf( '<span class="total-pages">'. esc_html__( 'Page %1$s of %2$s', 'codilight-lite' ) .'</span>', $current_page, $total_pages );
        echo '</div>';
    }
}
endif;

...and here is my customized category template:

<?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
    )
); 
        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-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; ?>

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

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

1 Answer 1

1

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='./?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.

8
  • Thanks, I tried that but it's not working. I'm testing it on a category with 5 child categories. I set it to show only 3 but it's showing all 5.
    – jrcollins
    Aug 24, 2016 at 7:50
  • I thought maybe I could create a new function based on the "codight" function so the styles match.
    – jrcollins
    Aug 24, 2016 at 7:51
  • Styling is not a problem. Just style the pagination as the theme. @jrcollins
    – CodeMascot
    Aug 24, 2016 at 16:10
  • Is pagination not working because I'm listing categories and not posts or because I'm using a custom template?
    – jrcollins
    Aug 25, 2016 at 1:09
  • Kind of.... Actually the main reason is the, the default pagination is for WP_Query. But here we are not using that. So the default pagination will not work here. @jrcollins
    – CodeMascot
    Aug 25, 2016 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.