最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom post type pagination 404 fix?

programmeradmin3浏览0评论

Been trying to get this working all day but haven't had any luck...

I have a custom post type called 'news' and an archive template (archive-news.php), here I'd like to show 2 posts with pagination, which works fine, until I try to go to the 'next page' (/news/page/2) which returns an error 404.

Any idea how to fix this / what I'm doing wrong?

I've literally spent all day trying to find a solution to this by searching Google and the WordPress forms, I found lots of solutions, none of which have worked so far.

My custom post type function:
My rewrite rules print:

Been trying to get this working all day but haven't had any luck...

I have a custom post type called 'news' and an archive template (archive-news.php), here I'd like to show 2 posts with pagination, which works fine, until I try to go to the 'next page' (/news/page/2) which returns an error 404.

Any idea how to fix this / what I'm doing wrong?

I've literally spent all day trying to find a solution to this by searching Google and the WordPress forms, I found lots of solutions, none of which have worked so far.

My custom post type function: http://pastebin.com/uG1L6YNu
My rewrite rules print: http://pastebin.com/jbaDANYr

Share Improve this question asked Jul 10, 2011 at 1:09 Dale-AnthonyDale-Anthony 3191 gold badge2 silver badges6 bronze badges 1
  • oh and also... My news-archive.php file: pastebin.com/vjcx77F7 – Dale-Anthony Commented Jul 10, 2011 at 1:09
Add a comment  | 

4 Answers 4

Reset to default 21

This is now working for me... for those of you having the same issue it turns out all the code was correct.

The problem was WordPress is setup to show 10 posts per page by default which clashed with my query (limiting it to 2 posts) to fix the issue I changed the WordPress setting (Settings / Reading in the admin dashboard) to 1.

I found a solution here : http://walrusinacanoe.com/web-development/742

It is elegant and functional :

add_action( 'parse_query','changept' );
function changept() {
    if( is_category() && !is_admin() )
        set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
    return;
}

You have probably tried this, but resetting the permalinks can solve this problem. Go to Settings->Permalinks and save the permalinks again.

try this variant:

add_action('pre_get_posts','custom_pre_get_posts'); 
function custom_pre_get_posts( $query ) 
{  
    if( $query->is_main_query() && !$query->is_feed() && !is_admin()) {
        $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) );  
    }  
} 

add_filter('request', 'custom_request');
function custom_request($query_string )
{ 
    if( isset( $query_string['page'] ) ) { 
        if( ''!=$query_string['page'] ) { 
            if( isset( $query_string['name'] ) ) { 
                unset( $query_string['name'] ); 
            } 
        } 
    } return $query_string; 
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论