0

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.

  • 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
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.

  • Unfortunately, this removes the ability to insert any new posts as well (in the wp-admin section), not able to create new posts or see any existing posts. That whole section is gone now, only have the ability to edit/add Categories and Tags. – Solomon Closson Aug 30 '17 at 5:03
  • @SolomonClosson You are correct. This seems to have been the case since WordPress 4.8 (haven't tested further back though). Not sure what is causing this. I have created a new question but no feedback as of yet: wordpress.stackexchange.com/questions/287065 – Luke Dec 3 '17 at 23:17
0

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 :)

-1

Change

register_post_type('post', $args);

to

register_post_type('news', $args);

and it works really fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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