Monday, March 29th, 2010, 683 days ago

Paging wordpress posts without using plug-ins

Days ago i saw a post talking about ‘paging wordpress posts without using plug-ins‘ like pagenav. The way to integrate such codes into themes is easy, but with a little bug. 
First put this paging function in the theme’s functions.php

function kriesi_pagination($query_string){
global $posts_per_page, $paged;
$my_query = new WP_Query($query_string .”&posts_per_page=-1″);
$total_posts = $my_query->post_count;
if(empty($paged))$paged = 1;
$prev = $paged – 1;
$next = $paged + 1;
$range = 2; // only edit this if you want to show more page-links
$showitems = ($range * 2)+1;

$pages = ceil($total_posts/$posts_per_page);
if(1 != $pages){
echo “<div class=’pagination’>”;
echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? “<a href=’”.get_pagenum_link(1).”‘>最前</a>”:”";
echo ($paged > 1 && $showitems < $pages)? “<a href=’”.get_pagenum_link($prev).”‘>上一页</a>”:”";

for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? “<span class=’current’>”.$i.”</span>”:”<a href=’”.get_pagenum_link($i).”‘ class=’inactive’ >”.$i.”</a>”;
}
}

echo ($paged < $pages && $showitems < $pages) ? “<a href=’”.get_pagenum_link($next).”‘>下一页</a>” :”";
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? “<a href=’”.get_pagenum_link($pages).”‘>最后</a>”:”";
echo “</div>n”;
}
}

Then put this reference code in the place where page navigation is needed to show.

<?php kriesi_pagination($query_string); ?>

And finally add some styles in the theme’s style.css file.

.pagination{
line-height:23px;
}
.pagination span, .pagination a{
font-size:12px;
margin: 2px 6px 2px 0;
background:#fff;
border:1px solid #e5e5e5;
color:#787878;
padding:2px 5px 2px 5px;
text-decoration:none;
}
.pagination a:hover{
background: #8391A7;
border:1px solid #fff;
color:#fff;
}
.pagination .current{
background: #fff;
border:1px solid #8d8d8d;
color:#393939;
font-size:12px;
padding:2px 5px 2px 5px;
}

Here comes a problem. When that is put in a new page to navigate posts, it does not work. And after carefully checking, the problem gets located in the function calling where the  ’query_string’ is used. If it is going to be used in a page with its ID equals 99, then the ‘query_string’ should be like this:

kriesi_pagination(“cat=99″);

or else, the page navigation would not show up due to the post number is zero.


3 Responses to “Paging wordpress posts without using plug-ins”

  1. Bryce Wisekal says:

    Show a little respect and credit the person you’ve snagged this source from. It’s obviously not yours as you can clearly see the function named after Kriesi, who is indeed a fantastic designer and coder who participates at ThemeForest.net.

    Don’t suppose that’d be asking too much, but then again who knows… *sighs* People stealing codes like this without crediting the original source is the reason so many people don’t share things as freely as they once did. A shame.

    • Andol says:

      @Bryce Wisekal Please watch your mouth before you comment anything inappropriately. This post has clearly noted and linked where i got the source code in the first sentence. I d be appreciate you to provide me very original source of the codes, but obviously this is not stealing, that is exactly the reason why i still keep the function name ‘Kriesi’.

  2. WP Themes says:

    Amiable dispatch and this fill someone in on helped me alot in my college assignement. Say thank you you seeking your information.

Leave a Reply

You can use these XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>