Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Can you paginate my custom post loop? WordPress


We have a custom post type loop for a client's project. This portfolio CPT needs to display X number of posts per loop. The number will change depending upon which version of the portfolio template the client is using.

I am looking for a finished and tested solution that does not involve a plugin. The number of posts displayed at one time should be defined via theme option function; the format we use is included.

Thanks guys!

Current loop
<?php query_posts(array('post_type' => array('portfolio', 'orderby'=> 'menu_order'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<div><?php next_posts_link('&laquo; Older Entries') ?></div>
<div><?php previous_posts_link('Newer Entries &raquo;') ?></div>
<?php endif; ?>

Theme option format

Answers (2)


Maor Barazany answers:

Currently you can't paginate a custom post type without a plugin if you use permalinks.
Only if you use the WP default, you can paginate.

That is a known issue on WP3.0 and 3.0.1, they just didn't create a them hierarchy file for custom posts type, and thus all of these issues.
I think I saw in WP Dev blog that they intend to extend the template hierarchy in WP3.1 to include also file for custom-post-type's archive page, and then it will be simple as having pagination to any other posts archives.
Till 3.1, the [[LINK href=""]]Simple Custom Post Type Archives[[/LINK]] just do the job of having this template file hierarchy enabled and doing the correct rewrites that should be done for this to work.

After activating it, create a template file to show your "archive" custom-post-type.
Take the archive.php (or index.php etc) from your theme, duplicate it and rename the file name to <strong>type-slug.php</strong>, Where <strong>slug </strong>is the value you gave as slug parameter when you registered your post type:

'rewrite' => array('slug' => 'news'),

i.e. if the slug is news, the filename would be type-news.php

Then, in the php file you created, find the loop (the line that starts with

if (have_posts()) : ?>

<?php while (have_posts()) : the_post();

and just before it add this line:


Of course, put your post type name you really have, and change the posts_per_page you want. If you want the number of posts per page to be dynamic via your option page, just call it and assign it's value:

$per_page = $settings['shortname_option_name'];


Oleg Butuzov answers:

yes why not.
what is page name (page slug ) that you had attache this post type query loop?