Pagination for WordPress without Plugin

When i started the creating process of this theme i considered to integrate one of the popular WordPress plugins for pagination. I than  decided to publish the XSimulator theme for free and i felt that it could lead to any problems and issues for someone else to make a similar looking theme when the requirements  are a lot of different plugins he must install first. Fortunately i found a great programmed script which is very easy to implement and powerful.

Open your functions.php and create a new function:

/* Adds pagination 
 * 
 * @since XSimulator 1.0
 * @param $range total list of pages 
 * @param $range count of maximum page links from left and right side of the current page link.
 * @bool $show_one_page_hint shows a text like: page 1 of 6
 * @bool $show_one_pager Do not show nav arrows
 * 
 */
function xsimu_pagination($range = 3, $show_one_pager = true, $show_page_hint = false)
{
    global $wp_query;
    $num_of_pages = (int)$wp_query->max_num_pages;

    if(!is_single() && $num_of_pages > 1)
    {
        $current_page = get_query_var('paged') === 0 ? 1 : get_query_var('paged');
        $num_of_display_pages = ($range * 2) + 1;        

        $output = '<div id="pagination">';

        if($show_page_hint)
        {
            $output .= '<span>Page ' . $current_page . ' of ' . $num_of_pages . '</span>';
        }

		if($current_page > 2 && $current_page > $range + 1 && $num_of_display_pages < $num_of_pages)
		{
			$output .= '<a href="' . get_pagenum_link(1) . '" title="Page 1 - Latest article">«</a>';
		}
		if($show_one_pager && $current_page > 1)
		{
			$output .= '<a href="' . get_pagenum_link($current_page - 1) . '" title="Page ' . ($current_page - 1) . ' - Latest Artikel">‹</a>';
		}

		for($i = 1; $i <= $num_of_pages; $i++)
		{
			if($i < $current_page + $range + 1 && $i > $current_page - $range - 1)
            {
				if($current_page === $i)
				{
					$output .= '<span class="current">' . $i . '</span>';
				}
				else
				{
					$output .= '<a href="' . get_pagenum_link($i) . '" title="Page ' . $i . '" >' . $i . '</a>';
				}
            }
		}

		if($show_one_pager && $current_page < $num_of_pages)
		{
			$output .= '<a href="' . get_pagenum_link($current_page + 1) . '" title="Page ' . ($current_page + 1) . ' - Older article">›</a>';
		}
		if($current_page < $num_of_pages - 1 && $current_page + $range < $num_of_pages && $num_of_display_pages < $num_of_pages)
		{
			$output .= '<a href="' . get_pagenum_link($num_of_pages) . '" title="Page ' . $num_of_pages . ' - Older article">»</a>';
		}

		$output .= '</div>';

        return $output;

    }
}

Open the template file where you want to publish your pagination. E.g. category.php

echo xsimu_pagination(2, true, false);

You´re ready:)

Thanks to the author of the original script on the page smart-webentwicklung.de

Written by

"Enthusiastic Sim-Racer from the north of germany. I write here about web development, hardware, software and latest motion simulator technologies."