How to paginate results with Custom Post Types on

2019-05-15 05:38发布

this has been asked before on WP forums, but none of the solutions worked for me, I've reset the permalinks, etc. to no avail. I hope you guys have the answer. This is my setup:

I have a custom post type called ted_venue, and two templates, single-ted_venue.php (single venue details) and page-venues.php (for displaying a list of venues). Permalink structure is site.com/venues/venue-slug for venue detail and site.com/venues for venues list.

This is the pagination code on list of venues:

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$wp_query = new WP_Query(array('post_type' => 'ted_venue',
'paged' => $paged,
'posts_per_page' => 30)
);

if ($wp_query->have_posts()):

?>

<ul>
<?php while ($wp_query->have_posts()): $wp_query->the_post(); ?>
...

Then at the bottom I add a few pagination links. If I go to site.com/venues/page/2 it brings a 404 error.

Then, if I reset permalinks to default and then to current, venue-list pagination works OK but single venue detail doesn't. So, basically when pagination works single venue doesn't and viceversa.

Does anyone have a clue on what may be happening or at least can you point me to an article or live working example?

Thanks!

标签: php wordpress
2条回答
何必那么认真
2楼-- · 2019-05-15 06:03

Ok, here's why it's not working:

You're pulling in 'paged' while WordPress still thinks that it's on a static page, therefore paged will always be 1. Unfortunately, there's no way to do what you want to do the way you're doing it right now, but there are other solutions.

There are basically two ways to fix this: First, you could override the rewrite rules your post type is generating yourself (this is the best way, but also the most complicated, and would involve days of work just figuring out how the rewrite engine really works). The second way (the easy way that also happens to work perfectly but just isn't necessarily the best way) is to use Matt Wiebe's 'Smarter Custom Post Types' code found here:

http://somadesign.ca/projects/smarter-custom-post-types/

查看更多
▲ chillily
3楼-- · 2019-05-15 06:09

Try adding 'order_by' => 'menu_order', as that is the only difference I can see with your code and others who have custom post pagination working (second post).

查看更多
登录 后发表回答