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

custom post types - Wordpress monthly archive links result in 404

programmeradmin1浏览0评论

When I use the archive widget in a sidebar, it creates the normal monthly post archive listing like the following:

  • February 2014
  • January 2014
  • October 2013

and so on. Whenever I click on these links, I get a 404 page.

The urls generated by the archive widget for monthly listings are in this format:

www.mysite/pedestrian/2014/02/

However, manually entering the following url style works:

www.mysite/pedestrian/?post_type=news&year=2014&monthnum=02

This shows exactly what I would expect to see (all the posts from February 2014).

I believe that the following code will fix my issue, but I am at a loss as to where to put it.

// Add day archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/([0-9]{2})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]','top');

// Add month archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]','top');

// Add year archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/?",'index.php?post_type=news&year=$matches[1]','top');

So, am i on the right track and if so where do i put this code? Thanks!

When I use the archive widget in a sidebar, it creates the normal monthly post archive listing like the following:

  • February 2014
  • January 2014
  • October 2013

and so on. Whenever I click on these links, I get a 404 page.

The urls generated by the archive widget for monthly listings are in this format:

www.mysite/pedestrian/2014/02/

However, manually entering the following url style works:

www.mysite/pedestrian/?post_type=news&year=2014&monthnum=02

This shows exactly what I would expect to see (all the posts from February 2014).

I believe that the following code will fix my issue, but I am at a loss as to where to put it.

// Add day archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/([0-9]{2})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]','top');

// Add month archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/([0-9]{2})/?",'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]','top');

// Add year archive (and pagination)
add_rewrite_rule("pedestrian/([0-9]{4})/page/?([0-9]{1,})/?",'index.php?post_type=news&year=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule("pedestrian/([0-9]{4})/?",'index.php?post_type=news&year=$matches[1]','top');

So, am i on the right track and if so where do i put this code? Thanks!

Share Improve this question asked Feb 26, 2014 at 18:21 user47912user47912 212 bronze badges 4
  • What exactly is "pedestrian" in your URLs? Why do you think that code would address the issue? – Rarst Commented Feb 26, 2014 at 18:49
  • I don't know if the code will work.. but if you need to put it somewhere put it in functions.php – gdaniel Commented Feb 26, 2014 at 18:51
  • pedestrian is just one of the pages on the site. i just included it because i was copy and pasting from my site – user47912 Commented Feb 26, 2014 at 19:01
  • How did you go with this @user47912? Did you end up finding a solution? Did @Wrought_steel's solution posted below work for you? – Tim Malone Commented May 30, 2016 at 10:31
Add a comment  | 

1 Answer 1

Reset to default 1

It looks like you need to add your news custom post type to the query for archive pages (I had the same problem).

Codex: custom post types to query

Add this code to your functions

    // Show posts of 'post', and 'news' post types on archive page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_archive() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'news' ) );
    return $query;
}

Just check that your admin post listings are not affected.

发布评论

评论列表(0)

  1. 暂无评论