wordpress theming - Numeric Pagination on my custom category page -
i have create custom category page template numeric pagination problem not able view 3rd page posts.
here code add in function.php
function custom_pagination($numpages = '', $pagerange = '', $paged='') { if (empty($pagerange)) { $pagerange = 2; } global $paged; if (empty($paged)) { $paged = 1; } if ($numpages == '') { global $wp_query; $numpages = $wp_query->max_num_pages; if(!$numpages) { $numpages = 1; } } $pagination_args = array( 'base' => get_pagenum_link(1) . '%_%', 'format' => '/page/%#%', 'total' => $numpages, 'current' => $paged, 'show_all' => false, 'end_size' => 1, 'mid_size' => $pagerange, 'prev_next' => true, 'prev_text' => __('previous'), 'next_text' => __('next'), 'type' => 'plain', 'add_args' => false, 'add_fragment' => '' ); $paginate_links = paginate_links($pagination_args); if ($paginate_links) { echo "<nav class='custom-pagination'>"; echo "<span class='page-numbers page-num'>page " . $paged . " of " . $numpages . "</span> "; echo $paginate_links; echo "</nav>"; } }
what add in category-5.php
<?php /** * template name: custom page * custom page template file */ get_header(); ?> <div class="container"> <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $custom_args = array('post_type' => 'post', 'posts_per_page' => 2, 'paged' => $paged); $custom_query = new wp_query( $custom_args ); ?> <?php if ( $custom_query->have_posts() ) : ?> <!-- loop --> <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?> <article class="loop"> <h3><?php the_title(); ?></h3> <div class="content"> <?php the_excerpt(); ?> </div> </article> <?php endwhile; ?> <!-- end of loop --> <!-- pagination here --> <?php if (function_exists(custom_pagination)) { custom_pagination($custom_query->max_num_pages,"",$paged); } ?> <?php wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'sorry, no posts matched criteria.' ); ?></p> <?php endif; ?> </div> <?php get_footer(); ?>
my css
.custom-pagination span, .custom-pagination { display: inline-block; padding: 2px 10px; } .custom-pagination { background-color: #ebebeb; color: #ff3c50; } .custom-pagination a:hover { background-color: #ff3c50; color: #fff; } .custom-pagination span.page-num { margin-right: 10px; padding: 0; } .custom-pagination span.dots { padding: 0; color: gainsboro; } .custom-pagination span.current { background-color: #ff3c50; color: #fff; }
you need read wordpress pagination documentation. please read below docs , recreate template accordingly
https://codex.wordpress.org/pagination https://codex.wordpress.org/function_reference/paginate_links
plugin:
https://wordpress.org/plugins/wp-paginate/
code examples;
functions.php
function pagination($pages = '', $range = 4) { $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages) { $pages = 1; } } if(1 != $pages) { echo "<div class=\"pagination\"><span>page ".$paged." of ".$pages."</span>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« first</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ previous</a>"; ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>"; } } if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">next ›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>last »</a>"; echo "</div>\n"; } }
style:
.pagination { clear:both; padding:20px 0; position:relative; font-size:11px; line-height:13px; } .pagination span, .pagination { display:block; float:left; margin: 2px 2px 2px 0; padding:6px 9px 5px 9px; text-decoration:none; width:auto; color:#fff; background: #555; } .pagination a:hover{ color:#fff; background: #3279bb; } .pagination .current{ padding:6px 9px 5px 9px; background: #3279bb; color:#fff; }
and use following function inside template, need show pagination
<?php if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } ?>
examples: http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
Comments
Post a Comment