I will not tell you that using get_posts() is the right thing to do...but here is some basic pagination code I setup using get_posts().
EDIT: As Pieter pointed out, this isn't meant to be used in production code. But just something I was playing around with anyway to see if I could get pagination working with get_posts(). If you are in a production environment you wouldn't want to use this.
$cpt_name = 'post-type'; //add your own post type
//what pagination page are we on?
if(! empty($_GET['pag']) && is_numeric($_GET['pag']) ){
$paged = $_GET['pag'];
}else{
$paged = 1;
}
//you could use this if you want, just make sure to use "/page/2" instead of "?pag=2" in the pagination links.
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
//how many posts should we display?
$posts_per_page = (get_option('posts_per_page')) ? get_option('posts_per_page') : 10;
//let's first get ALL of the possible posts
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'fields' => 'ids',
'post_type' => $cpt_name
);
$all_posts = get_posts($args);
//how many total posts are there?
$post_count = count($all_posts);
//how many pages do we need to display all those posts?
$num_pages = ceil($post_count / $posts_per_page);
//let's make sure we don't have a page number that is higher than we have posts for
if($paged > $num_pages || $paged < 1){
$paged = $num_pages;
}
//now we get the posts we want to display
$args = array(
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => $cpt_name,
'paged' => $paged
);
$my_posts = get_posts($args);
//did we find any?
if(! empty($my_posts)){
echo '<div id="my-posts-wrapper">';
//THE FAKE LOOP
foreach($my_posts as $key => $my_post){
//do stuff with your posts
echo '<div class="my-post">'.$my_post->post_title.'</div>';
}
echo '</div>';
//we need to display some pagination if there are more total posts than the posts displayed per page
if($post_count > $posts_per_page ){
echo '<div class="pagination">
<ul>';
if($paged > 1){
echo '<li><a class="first" href="?pag=1">«</a></li>';
}else{
echo '<li><span class="first">«</span></li>';
}
for($p = 1; $p <= $num_pages; $p++){
if ($paged == $p) {
echo '<li><span class="current">'.$p.'</span></li>';
}else{
echo '<li><a href="?pag='.$p.'">'.$p.'</a></li>';
}
}
if($paged < $num_pages){
echo '<li><a class="last" href="?pag='.$num_pages.'">»</a></li>';
}else{
echo '<li><span class="last">»</span></li>';
}
echo '</ul></div>';
}
}
I hope someone gets some use out of this :)
EDIT: What the heck! Going to do something the wrong way...might as well do it right! Here is some LESS as well (without any mixins).
.pagination { margin: 30px 0px;
ul { display:block; list-style-type:none; margin:0 auto; padding: 0px;
li { display:inline-block; list-style-type:none; margin:0; padding:0;
a, span { display:inline-block; font-size: 14px; width:auto; min-width:26px; height:26px; line-height: 26px; border: 1px solid #dddddd; border-right: 0px; background:#FFFFFF; color:#FF0000; padding: 5px; text-align: center;
&:hover { cursor:pointer; text-decoration:none; }
&.first { border-top-left-radius: 3px; border-bottom-left-radius: 3px; }
&.last { border-top-right-radius: 3px; border-bottom-right-radius: 3px;}
}
span.last,
span.first { color: #FF0000;
&:hover { cursor: default; }
}
a.last,
a.first {
&:hover { }
}
a:hover,
&.active a,
.current { background:#FF0000; color:#ffffff; border-color: #b21712; }
&:last-child {
a, span { border-right: 1px solid #dddddd; }
a {
&:hover { border-color: #FF0000; }
}
}
}
}
}