public
Created

Forces pagintation where pagination fails in Wordpress

  • Download Gist
MF_Query.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
class MF_Query extends WP_Query{
private $page_link = "page";
function __construct(array $args){
if(!array_key_exists('posts_per_page',$args)) $args['posts_per_page'] = 10;
if(array_key_exists('page_link',$args)) $this->page_link['page_link'];
 
$args['offset'] = (isset($_GET[$this->page_link])?($_GET[$this->page_link]-1)*$args['posts_per_page']:0);
parent::query($args);
}
 
function next($link_text = "Next"){
 
$curPage = intval((isset($_GET['page'])?$_GET['page']:1));//Use 1 if $_GET['page'] not set
 
$link = "<a href='".remove_post_vars(curPageURL());
 
if($curPage<$this->max_num_pages){
echo $link.$this->constructQuery($this->merge(array($this->page_link=>$curPage+1),$_GET))."'>".$link_text."</a>";
} else {
return false;
}
 
 
}
function prev($link_text = "Previous"){
 
$curPage = (isset($_GET['page'])?$_GET['page']:1);//Use 1 if $_GET['page'] not set
$link = "<a href='".remove_post_vars(curPageURL());
if($curPage>1){
echo $link.$this->constructQuery($this->merge(array($this->page_link=>$curPage-1),$_GET))."'>$link_text</a>";
} else {
return false;
}
 
 
}
private function constructQuery(array $query){
 
 
$url_ext = "?";
foreach($query as $k => $v){
$url_ext .=$k."=".$v."&amp;";
}
$url_ext = substr($url_ext, 0, -5);//chop last ampersand off
 
 
return $url_ext;
 
}
private function merge($get, $put){
//Get values from one array, and put them in another (overriding existing values if appropriate)
foreach ($get as $k => $v){
$put[$k]=$v;
}
return $put;
 
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.