Wordpress SEO paging navigation
I was not satisfied how my blog paging looks like, and I realized on the SEO side it would be better that I have more links to pages available, so search engine spiders can hit the articles with the less steps. So I found this guy http://robertbasic.com/blog/wordpress-paging-navigation/ and his code. It works flawless and it is right what I needed. However since I like to have my pages validated, and his code break validation I decided to fix it. You need to add this code into your link-template.php which is located in the wp-includes folder (p.s. you can find wordpress pager plugin but it is also not valiadated).
So here is his code validated:
/** * A pagination function * @param integer $range: The range of the slider, works best with even numbers * Used WP functions: * get_pagenum_link($i) - creates the link, e.g. http://site.com/page/4 * previous_posts_link(' « '); - returns the Previous page link * next_posts_link(' » '); - returns the Next page link */ function get_pagination($range = 8){ // $paged - number of the current page global $paged, $wp_query; // How much pages do we have? if ( !$max_page ) { $max_page = $wp_query->max_num_pages; } // We need the pagination only if there are more than 1 page if($max_page > 1){ if(!$paged){ $paged = 1; } // On the first page, don't put the First page link if($paged != 1){ echo '<a href="' . get_pagenum_link(1) . '"> First </a>'; } // To the previous page previous_posts_link(' « '); // We need the sliding effect only if there are more pages than is the sliding range if($max_page > $range){ // When closer to the beginning if($paged < $range){ for($i = 1; $i <= ($range + 1); $i++){ echo '<a href="' . get_pagenum_link($i) .' "'; if($i==$paged) echo 'class="current"'; echo ">$i</a>"; } } // When closer to the end elseif($paged >= ($max_page - ceil(($range/2)))){ for($i = $max_page - $range; $i <= $max_page; $i++){ echo '<a href="' . get_pagenum_link($i) .' "'; if($i==$paged) echo 'class="current"'; echo ">$i</a>"; } } // Somewhere in the middle elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){ for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){ echo '<a href="' . get_pagenum_link($i) .' "'; if($i==$paged) echo 'class="current"'; echo ">$i</a>"; } } } // Less pages than the range, no sliding effect needed else{ for($i = 1; $i <= $max_page; $i++){ echo '<a href="' . get_pagenum_link($i) .' "'; if($i==$paged) echo 'class="current"'; echo ">$i</a>"; } } // Next page next_posts_link(' » '); // On the last page, don't put the Last page link if($paged != $max_page){ echo '<a href="' . get_pagenum_link($max_page) . '"> Last </a>'; } } }
After that you need to define in your theme index.php where the pagination will show itself, and you will do that with this piece of code:
<div class="navigation">
<?php get_pagination() ?>
</div>Now you only need to style it to fit your current wordpress theme. For example since we wrapedd pagination into div with class “navigation” add this classes to your css and adopt the variables:
.navigation { display: block; text-align: center; margin-top: 10px; margin-bottom: 60px; font-size:80%; border:1px solid #000000 } .navigation a{ text-align:center; padding:5px 10px; margin:0 2px 0 2px; border:1px solid #000000; background:#ffffff; } .navigation a:hover{ background:#a90000; }
Example of the pagination you can see at this site blog section. And as I said again, credits for this code goes to this guy, I just validated the code.
Since this code because of copy/paste maybe have some extra spaces here you can download my link-template.php. It is from wordpress 2.7.1
Related posts:
- Wordpress SEO theme Here is theme used on this blog as I promised....
- Must have plugins for Wordpress Since Sulumits Retsambew contest is going toward its end,...
[...] SEO paging navigation Here is how to manualy add paging to your wordpress blogs. Wordpress SEO paging navigation | Sulumits Retsambew You can see it in action at the bottom of the Sulumits Retsambew There is bunch of plugins out [...]
Pingback by Wordpress SEO paging navigation - Net Builders — May 2, 2009 @ 11:01 am
Hellas, How much size will the whole code add to your website ?
Comment by sulumits retsambew — May 2, 2009 @ 10:38 pm
Hmm. Almost nothing. It ads 6 lines of code to the final blog page output.
Just check the blog page. Find div navigation.
Comment by admin — May 2, 2009 @ 10:45 pm
hey this is a very interesting article!
Comment by KeHoeff — May 29, 2009 @ 1:34 am
Thanks! This is very helpful! I have a question though. Is there any way to let visitors know what page they’re on? How can I style the current page differently?
Comment by John — September 9, 2009 @ 1:41 am
Ah, nevermind! I just realized the class “current” automatically gets added to the current page!
Comment by John — September 9, 2009 @ 1:43 am
I’m sorry to bother you but I have one question. Is there a way to keep the ‘first’ and ‘last’ links from disappearing when on the first and last pages?
Comment by John — September 9, 2009 @ 2:27 am
you should try wp-pagenavi plugin
guy validate it and you might have all this needed option and you will not lose your changes after the wordpress upgrade.
Comment by Hellas — September 10, 2009 @ 3:24 pm
Well, i tried this insted of wp-pafenavi and it rocks! It’s far better for perfomance to use wp internal functions instead of plugins – thank u !
Comment by Dzhipp — February 4, 2010 @ 6:07 am
Since this code because of copy/paste maybe have some extra spaces here you can download my link-template.php. It is from wordpress 2.7.1
Comment by seamless steel pipe — February 22, 2010 @ 9:56 am