Here is a small… yes, a small function for displaying a paged navigation in your WordPress powered blog without using any plugin.
The function is self-explaining, it accepts one optional parameter, the number of links to show without counting the previous and next buttons.
It accepts your language
The first, previous, next and last buttons text can be set to anything you want from within the function, you probably don’t want to change the presets, the arrows work just fine and don’t need any translation, but it you want to put something else and if you are making a template that is meant for distribution, I advise that you use the __() function, example:
$first_txt = __('First', 'theme_textdomain'); $previous_txt = __('Previous', 'theme_textdomain'); $last_txt = __('Last', 'theme_textdomain'); $next_txt = __('Next', 'theme_textdomain');
The function
This is the function:
/** * get_paged_navigation displays a paged navigation * @param int $number_of_pages_to_show * @author Nabil Kadimi * @link https://kadimi.com/ */ function get_paged_navigation($number_of_pages_to_show = 10){ $current_page_number = (int) $GLOBALS['paged']; $number_of_pages = (int) $GLOBALS['wp_query']->max_num_pages; $first_txt = '«'; $previous_txt = '‹'; $next_txt = '›'; $last_txt = '»'; if($number_of_pages < 2) return true; if(!$current_page_number) $current_page_number = 1; $last_head_page_number = ceil($number_of_pages_to_show/2); $firt_tail_page_number = $number_of_pages - floor($number_of_pages_to_show/2); if($current_page_number = $firt_tail_page_number) $page_first_shown = $number_of_pages - $number_of_pages_to_show + 1; else $page_first_shown = $current_page_number - ceil($number_of_pages_to_show/2) + 1; // First arrows if($page_first_shown != 1){ echo '', $first_txt, ' '; echo get_previous_posts_link($previous_txt), ' '; } // Linked page numbers for($i=0; $i < $number_of_pages_to_show && $i<$number_of_pages; $i++) echo '', $page_first_shown + $i, ' '; // Last arrows if($page_first_shown + $number_of_pages_to_show < $number_of_pages + 1){ echo get_next_posts_link($next_txt), ' '; echo '', $last_txt, ' '; } }
Usage
get_paged_navigation();
With formatting
One more thing, if the pagination will have any formatting surrounding it, you can have a conditional statement so that the formatting doesn’t appear if the pagination is not needed (there is only 1 page), here is an example:
if($GLOBALS['wp_query']->max_num_pages > 1){ echo ''; }