admin管理员组文章数量:1291757
Finally got pagination to work with the below code, but now the pagination links to /videos/page/2/, which doesn't exist. How do I get page 2 to work?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=videos&showposts=1'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
//display stuff
endwhile;
php wp_pagenavi();
?>
Finally got pagination to work with the below code, but now the pagination links to /videos/page/2/, which doesn't exist. How do I get page 2 to work?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=videos&showposts=1'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
//display stuff
endwhile;
php wp_pagenavi();
?>
Share
Improve this question
edited Mar 5, 2012 at 23:55
Dave
asked Mar 5, 2012 at 23:30
DaveDave
8104 gold badges9 silver badges21 bronze badges
2
|
5 Answers
Reset to default 21Found the answer:
After a looong day debugging thru wordpress core, I managed to solve this issue.
Basicly, you CANT have a PAGE and a CUSTOM POST TYPE with the same name. If you do, the permalink rewrite rules will get confused and trigger a 404.
A very simple solution I'm using is: The page that lists the custom post types is called in plural (eg. products) and the actual post type name is in singular (eg. product). So they dont conflict and it's all fine.
Done Done! Hope this will save people's time.
- via rafaelxy (http://wordpress/support/topic/pagination-with-custom-post-type-listing)
After a long time I found a solution for this issue (thanks to franzblog).
If you are using version 4.2 or higher, you need to add the following lines in your functions.php
file:
add_filter( 'redirect_canonical', 'custom_disable_redirect_canonical' );
function custom_disable_redirect_canonical( $redirect_url ) {
if ( is_paged() && is_singular() ) $redirect_url = false;
return $redirect_url;
}
Everything is working fine now!
you CANT have a PAGE and a CUSTOM POST TYPE with the same name
Maybe tree years ago it was impossible, but now you CAN.
First, add this lines to $args
into your post type:
'has_archive' => false,
'rewrite' => array(
'slug' => 'your slug', // if you need slug
'with_front' => false,
),
Second, in functions.php
add action:
add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
global $wp_post_types;
foreach ($wp_post_types as $wp_post_type) {
if ($wp_post_type->_builtin) continue;
if (!$wp_post_type->has_archive && isset($wp_post_type->rewrite) && isset($wp_post_type->rewrite['with_front']) && !$wp_post_type->rewrite['with_front']) {
$slug = (isset($wp_post_type->rewrite['slug']) ? $wp_post_type->rewrite['slug'] : $wp_post_type->name);
$page = get_page_by_slug($slug);
if ($page) add_rewrite_rule('^' .$slug .'/page/([0-9]+)/?', 'index.php?page_id=' .$page->ID .'&paged=$matches[1]', 'top');
}
}
}
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
return ($page ? get_post($page, $output) : NULL);
}
Don't forget to flush rules in dashboard.
One possibility is that, on the admin dashboard Settings -> Reading
, if you have selected a Posts page
, the option Blog pages show at most
will conflict with your custom posts_per_page
query in that page.
So make sure that you either unselect the posts page, or make sure the posts per page matches the Blog pages show at most
option in Settings -> Reading
.
After debugging a long time even I got the solution regarding pagination issue
Basically, In WordPress, you can't have a PAGE and a CUSTOM POST TYPE with the same name. If you do, the permalink rewrite rules will get confused and trigger a 404.
本文标签: permalinksWhy is quotpage2quot not working
版权声明:本文标题:permalinks - Why is "page2" not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741540784a2384307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
? Then you can use the built in pagination code rather than writing your own, and you can simplify down to a standard post loop – Tom J Nowell ♦ Commented Feb 7, 2016 at 13:32/videos/
that will loadarchive-videos.php
, that requires no messing around with queries – Tom J Nowell ♦ Commented Feb 7, 2016 at 13:33