admin管理员组

文章数量:1336380

I have set the following custom permalink structure (for my blog):

/news/%postname%

When I browse to /news I was expecting for the archive.php template to be served up, unfortunately, I'm presented with a 404.

My rewrite is working properly since I can view all posts such as /news/my-post-title.

How can I make it so that my "archive.php" is served up when browsing to /news, currently I have to create a dummy News page which is of course what I don't want.

Any help would be much appreciated.

UPDATE:

I have set my Category Base and Tag Base permalinks as follows:

news/categorized
news/tagged

Browsing to /news/categorized/test triggers the category.php template and browsing to /news/tagged/test brings me to the tag.php template.

I still have no luck in triggering the archive.php.

I have set the following custom permalink structure (for my blog):

/news/%postname%

When I browse to /news I was expecting for the archive.php template to be served up, unfortunately, I'm presented with a 404.

My rewrite is working properly since I can view all posts such as /news/my-post-title.

How can I make it so that my "archive.php" is served up when browsing to /news, currently I have to create a dummy News page which is of course what I don't want.

Any help would be much appreciated.

UPDATE:

I have set my Category Base and Tag Base permalinks as follows:

news/categorized
news/tagged

Browsing to /news/categorized/test triggers the category.php template and browsing to /news/tagged/test brings me to the tag.php template.

I still have no luck in triggering the archive.php.

Share Improve this question edited Feb 24, 2012 at 22:26 Luke asked Feb 23, 2012 at 6:21 LukeLuke 5912 gold badges9 silver badges21 bronze badges 9
  • is news a custom post type? or just the page for your blog? – Alex Older Commented Feb 23, 2012 at 11:47
  • This is just for built-in posts, not a custom post type. For some reason, custom post types seem to handle this just fine. – Luke Commented Feb 23, 2012 at 20:20
  • Do you have a page called news? – Alex Older Commented Feb 24, 2012 at 9:35
  • I don't have a page called "news". It's the whole point that I don't have to create "dummy" pages. I was expecting that my "archive.php" would be triggered instead. – Luke Commented Feb 24, 2012 at 21:32
  • if you browse to news/archive does it work? – Alex Older Commented Feb 25, 2012 at 9:48
 |  Show 4 more comments

4 Answers 4

Reset to default 1

You can override the Post type object settings in the init action and register the proper values for the archive and rewrites:

add_action('init', 'my_init');

function my_init()
{
   $args = objectToArray( get_post_type_object('post') );

   $args['has_archive'] = 'news';
   $args['rewrite'] = array(
      'slug' => 'news',
      'with_front' => FALSE,
   );

   register_post_type('post', $args);
}

function objectToArray( $object )
{
   if( !is_object( $object ) && !is_array( $object ) )
   {
      return $object;
   }

   if( is_object( $object ) )
   {
      $object = get_object_vars( $object );
   }

   return array_map('objectToArray', $object);
}

By specifying the has_archive and the rewrite values for the Post type object it's possible to force Wordpress the same behavior as one would do for Custom Post types.

Wordpress should now pick up the archive.php template when browsing to /news.

The only clumsy thing about this is of course that get_post_type_object returns an object version of the array that register_post_type takes as a parameter, hence the objectToArray helper function.

In case you want archive page like /news, need custom slug after archive page like /news/latest and post detail page like /news/latest/%postname%, Below is code that helps me..

add this code to function.php

add_action( 'init', 'create_news' );
function create_news() {
register_post_type( 'news',
    array(
           'labels' => array(
            'name' => 'News',
            'singular_name' => 'News',                
          ), 
          'taxonomies' => array( 'category'),
          'rewrite' => array( 'slug' => '%latest_tag%', 'with_front' => false ),
          'has_archive' => 'news'
        )
    );
    register_taxonomy(
         'latest_tag',
         'news',
         array(
            'rewrite' => array( 'slug' => 'latest', 'with_front' => false ),
          )
      );
}

after that, put below code in same function.php..

function custom_slug_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'news' ){
        $terms = wp_get_object_terms( $post->ID, 'state_tag' );
        if( $terms ){
           return str_replace( '%latest_tag%' , $terms[0]->slug , $post_link );
        }
  }
   return $post_link;
  }
 add_filter( 'post_type_link', 'custom_slug_permalinks', 1, 2 );

There is no need to add 'has_archive' as true, instead you can use custom post slug name over there and wordpress will go to archive.php.. After all that, Do not forget to go settings->permalinks and click on save button :)

Re-registering the post type was causing me errors so instead you can filter the "args" on post creation (as opposed to recreating the whole post type).

add_filter('register_post_type_args', function($args, $type) {
    if ($type === 'post') {

        $args['has_archive'] = 'news';
        $args['rewrite'] = array(
            'slug' => 'news',
            'with_front' => FALSE,
        );
    }
    return $args;
}, 10, 2);

Again, you still need to go settings->permalinks and click save.

You can see the filter in action here: https://github/WordPress/WordPress/blob/fcd890e54625d165c853825cdd4a44d17afd2acd/wp-includes/class-wp-post-type.php#L386

Hook documentation here: https://developer.wordpress/reference/hooks/register_post_type_args/

Change

register_post_type('post', $args);

to

register_post_type('news', $args);

and it works really fine!

本文标签: Post custom permalink results in 404 for archive page