1

Can anyone tell me how to create a function out of the following code? It's for displaying page navigation links in a custom category template. As you can see, it's quite a large block of code and I would like to wrap it in a function so I can generate the links with just a single line of code in my template.

Here it is:

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

And here's the the 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 = 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(); ?>
1

You need to pass it at least 2 parameters, or it won't know what to look for:

<?php
function custom_pagination($cat_id, $catnum) {
    // 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
}
?>

Then, in your template you can use the function adding the values dynamically:

$cat_id = 3;
$cat_num = 5;

custom_pagination($cat_id, $catnum);
  • I tried this and the links are showing but aren't fully functional. It might be necessary to add more arguments? I've updated my question to show the custom category template so you can see what variables are used for the page links. – jrcollins Aug 30 '16 at 5:29
  • What do you mean with non fully functiona? – The J Aug 30 '16 at 9:33
  • It's working now. I just needed to add an extra argument. Thanks for your help. – jrcollins Aug 30 '16 at 10:53
  • was it $pages ? – The J Aug 30 '16 at 10:54
  • I edited my question to include all the code from my custom template. – jrcollins Aug 30 '16 at 11:02
1

I just needed to add an extra argument to the function to get it to work. This is what's working:

function custom_pagination($cat_id, $catnum, $catpage) {
//page navigation code here
}

and in the custom category template:

<?php custom_pagination($cat_id, $catnum, $catpage); ?>
  • Either update your question with new information, or ask a new question. Do not use an answer to propose new questions. – Greg McMullen Aug 30 '16 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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