Quantcast
What was you're answer out of curiosity? BaseClass::StaticFunction() -> DerivedClass::StaticFunction()? namespace conflict?

My name is Michael Kofman and
I am a Developer, a Designer, an Architect, an Entrepreneur, an Educator and a Student.

Graduated Full Sail University with a Bachelors of Science in Game Development with a life long background in IT and Web Design.

Wordpress Pagination

Today while striving to wrap up Michael Kofman 2009 I found myself spending longer than I would have liked trying to get a “wordpress page” to loop through all of my blog posts with enabled pagination. The intention was to become familiar with the wordpress framework, install a few plugins and be on my marry way. Sadly things didn’t quite go so smoothly. So to make this a lot less painful for future readers here is a short list of steps and plug ins used as well as a final code snippet. After I’ll discuss some of the alternate methods I found, share my work cited and hopefully put some value into the community.

Wordpress Page Templates

My first question was how to create a new page in wordpress and apply to it my own layout and stylesheets. This was incredibly easy and intuitive.

  1. Create a new .php page that will serve as a template for your new wordpress page.
  2. Page templates are easily recognized by the wordpress CMS via a magical comment.
1
2
3
4
5
<?php
/*
Template Name: Your Template Name Here
*/
?>
  1. Navigate to /wp-admin/ and depending on the version of your dashboard click add page.
  2. Here you can give your page any title you want. And somewhere on the page you should see an option to apply a template to the page (again this varies with wordpress versions so Google or check documentation). If you uploaded your new .php template to the proper location you should conveniently see “Your Template Name Here” as an option.

Installing a Pagination Plugin

I decided to use WP-PageNavi although there are several alternatives I’ve found.

To download and install follow these instructions:

1. Download WP-PageNavi
2. Upload folder “pagenavi” under wp-contents/plugins
3. Activate the plugin
4. Add the following code into your current theme’s php file.

5. Configure the settings under WP-Admin -> Options -> PageNavi
6. Click “Update Options” and you are done.

The Lonely Loop

The reason I titled this lonely loop is because most wordpress documentation will refer to the post entries as “THE LOOP“. The final code snippet that finally got things rolling for me was as follows:

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
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
 
		$my_query = new WP_Query('showposts=5&paged=$page');
		$wp_query = $my_query;
 
		query_posts("showposts=5&paged=$page");
 
		while ( have_posts() ) : the_post() ?>
  			<div class="post" id="post-<?php the_ID(); ?>">
                    <div class="date">
                    	<div class="day"><center><?php the_time('d') ?></center> </div>
                    	<div class="month"><center><?php the_time('F') ?></center> </div>
                    	<div class="year"><center><?php the_time('Y') ?></center> </div>
                    </div>
 
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
 
                    <?php the_content('Read the rest of this entry &raquo;'); ?>
 
                    <p><?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
                </div>
                <br class="clear" />
                <br class="clear" />
		<?php endwhile; ?>
 
 
       <div><?php next_posts_link('Older Entries')?></div>
       <div><?php previous_posts_link('Newer Entries')?></div>
 
        <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

Obstacles Overcome

One of the first challenges I faced was realizing that the default loop provided in index.php did not properly render posts but instead interpreted function calls like the_title() with regard to the title of the page (template) opposed to individual posts. I later found according to slightly misleading documentation that a new query object is required. I proceeded to create one called $my_query. Where I would prefix function calls as such.

$my_query->have_posts()

and

$my_query->the_post()

This seemed to work, but the next problem was getting pagination to work in synch with this new query. The problem was that WP-PageNavi only listens to $wp_query. I had wondered around for some time trying to resolve this particular delima short of writing ajax and the pagination functionality by hand or maybe trying a different plug in. Thank fully after jumping from link to link I finally stumbled onto the following code block.

<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("showposts=5&paged=$page");
while ( have_posts() ) : the_post() ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<span class="entry-date"><?php the_time('F d, Y'); ?></span>
<?php the_content(); ?>
<?php endwhile ?>

This finally got the ball rolling. I had only correct a few errors that sparked from the constant experimentation like a random reseting of the query at the end of the while loop. Conveniently above the pagination caused the current page to always read 1.

Hope this will be helpful to someone in the future. Here is an unordered list of sources.
http://codex.wordpress.org/Template_Tags/query_posts
http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/Function_Reference/WP_Query
http://codex.wordpress.org/IRC
http://almosteffortless.com/2006/12/21/wordpress-pagination-on-a-page/
http://jarodtaylor.com/blog/wordpress-ultimate-archive-index-with-pagination/
http://www.w3cgallery.com/w3c-css/wordpress-pagination-in-pagephp-or-display-subpages-as-paging-under-parent-page
http://wordpress.org/support/topic/256899
http://www.lesterchan.net/wordpress/readme/wp-pagenavi.html

3 Responses to “Wordpress Pagination”

  1. Mark Mann Says:

    February 5th, 2010 at 8:56 am

    Thank you SO much! I’ve spent way too many hours on trying to figure this out, until I found this post. THANK YOU!!

  2. Peng Says:

    July 13th, 2010 at 8:13 pm

    Great article and contribution for the community. Thank you.

  3. Bill Seymour Says:

    October 21st, 2010 at 11:47 am

    Michael-

    Excellent piece of problem-solving. I’d come to the same obstacles you described, but lacked the coding knowledge to properly implement a solution.

    Also: plugin WP-Paginate works successfully with your code, placing the “if function exists” code for wp-paginate in the same location as your code for wp-page navi.

    Thanks for the solution and expanding my knowledge of wp, php, etc. –Bill




Please Reply








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