Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
is news a custom post type? or just the page for your blog? –  Alex Older Feb 23 '12 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 Feb 23 '12 at 20:20
    
Do you have a page called news? –  Alex Older Feb 24 '12 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 Feb 24 '12 at 21:32
    
if you browse to news/archive does it work? –  Alex Older Feb 25 '12 at 9:48
show 5 more comments

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.