Bootstrap 4 – Pagination for WordPress

This code has been created for archive.php page.

        $terms = get_the_terms($post->ID,'category'); 
        foreach( $terms as $cat ){
            $cat_id = $cat->term_id;
            $cat_name = $cat->name;
        }
        //$show_posts = '2';
        $cat_name = $cat_name;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $my_query = new WP_Query( array(
            'category_name' => $cat_name,
            //'posts_per_page' => $show_posts,
            'paged' => $paged,
            'orderby' => 'menu_order', 
            'order' => 'ASC',
        ) );

            echo '<ul class="eye-ul">';

            while ($my_query->have_posts()) : $my_query->the_post(); 
                //echo $post->menu_order .': ';
                the_title( '<li class="entry-list"><i class="fa fa-file-alt mt-1 mr-2"></i><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></li>' );

            endwhile; 

            echo '</ul>';

            $total_pages = $my_query->max_num_pages;
            if ($total_pages > 1){
                $current_page = max(1, get_query_var('paged'));
                echo '<nav class="text-center" aria-label="Page navigation" >';
                $pg = paginate_links(array(
                    'base' => get_pagenum_link(1) . '%_%',
                    'format' => 'page/%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'prev_text'    => __('« prev'),
                    'next_text'    => __('next »'),
                    //'type' => 'list'
                    'type'  => 'array'
                ));
                //print_r($pg);
                echo '<ul class="pagination">';
                foreach ( $pg as $page ) {
                    echo '<li class="page-item '.(strpos($page, 'current') !== false ? 'active' : '').'"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
                }
                echo '</ul>';
                echo '</nav>';
            }
            wp_reset_query();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>