Add Pagination to WordPress Theme Without Plugin
Adding pagination to a WordPress theme without plugins involves several steps. We'll go through these steps. Before adding pagination, subtheme I recommend you create a .
Adding CSS Codes to WordPress
Add the following CSS code to your theme's style.css file. You can customize the file to your liking.
/* Pagination */ .wp_paging { margin: 0 0 20px; padding: 0; } .wp_paging ul { margin: 0; padding: 0; list-style: none; } .wp_paging ul li { margin: 0; padding: 0; display: inline; } .wp_paging ul li a { float: left; display:block; color: #666; text-decoration: none; margin-right: 5px; padding: 5px 10px; background-color: #FFFFFF; border: 1px solid #999; } .wp_paging ul li a:hover { color: #090; } /* Pagination End */
Adding Functions to WordPress
Add the following function to your theme's functions.php file. You can customize the HTML code within the function.
/* Pagination */ function pagination($pages = '', $range = 2) { $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='wp_paging'><ul>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='/en/".get_pagenum_link(1)."/'>First</a></li>"; if($paged > 1 && $showitems < $pages) echo "<li><a href='/en/".get_pagenum_link($paged - 1)."/'>Previous</a></li>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<li><a href='#'>".$i."</a></li>":"<li><a href='/en/".get_pagenum_link($i)."/'>".$i."</a></li>"; } } if ($paged < $pages && $showitems < $pages) echo "<li><a href='/en/".get_pagenum_link($paged + 1)."/'>Next</a></li>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='/en/".get_pagenum_link($pages)."/'>End</a></li>"; echo "</ul><div class='cleaner'></div></div>"; } } /* pagination */
Adding Pagination
We add the page to the desired location in the theme using the code below.