Tente isto:
<?php
$link = "";
// $page = $_GET['page'];
// $pages=20; // Hardcoded for testing purpose
$limit=5 ;
if ($pages >=1 && $page <= $pages)
{
$counter = 1;
$link = "";
if ($page > ($limit/2))
{ $link .= "<a href=\"?page=1\">1 </a> ... ";}
for ($x=$page; $x<=$pages;$x++)
{
if($counter < $limit)
$link .= "<a href=\"?page=" .$x."\">".$x." </a>";
$counter++;
}
if ($page < $pages - ($limit/2))
{ $link .= "... " . "<a href=\"?page=" .$pages."\">".$pages." </a>"; }
}
echo $link;
?>
SAÍDA:
//At page=1
1 2 3 4 ... 20
//At page=12
1 ... 12 13 14 15 ... 20
//At page=18
1 ... 18 19 20