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 noticed that WP 3.1 supposedly has 'new CMS capabilities like archive pages for custom content types', however, I can't see that implemented yet?

I've been using a plugin called 'Simple Custom Post Type Archives' to view custom posts at the url http://www.domainname.com/custom-post-type/, but wanted to use the in-built capability considering it is 'now possible'.

Has anyone else had the same issue?

Thanks

osu

PS. I'm using archive-custom_post_type_name.php to try and style my custom post type archive page

share|improve this question

2 Answers 2

up vote 13 down vote accepted

Yes, you'll just need to set the has_archive parameter to true or your chosen slug when registering your custom post type.

So firstly add the has_archive parameter to your post type, here's an example...

add_action( 'init', 'question_10706_init' );

function question_10706_init() {

    register_post_type( 'example', array(
        'labels' => array(
            'name' => __('Examples'),
            'singular_name' => __('Example')
            ),
        'public' => true,
        'show_ui' => true,
        'rewrite' => array(
            'slug' => 'example',
            'with_front' => false
            ),
        //'has_archive' => true // Will use the post type slug, ie. example
        //'has_archive' => 'my-example-archive' // Explicitly setting the archive slug
    ) );

}

The has_archive parameter supports the following settings.

  1. false (default)

    No archive

  2. true

    The archive url is formulated from the post type slug

    www.example.com/example/

  3. 'string'

    The archive url is explicitly set to the slug you provided

    www.example.com/my-example-archive/

Once you've added the parameter visit the permalink page, this will cause a regeneration of the rewrite rules, accounting for the custom post type archive.

Lastly, create an archive-{$post_type}.php template to handle that archive (it could be a straight copy->paste of your existing archive, make adjustments as necessary).
Noting, that {$post_type} would of course represent the slug of your actual post type.

Sourced information:

Hope that helps. :)

share|improve this answer
    
Absolutely superb. Thanks so much, worked perfectly! –  Osu Feb 28 '11 at 15:46
    
You're welcome. :) –  t31os Feb 28 '11 at 17:35
    
It would be useful if there was a nice way to create a menu item to this as well; I have to use an external link, the menu for which then doesn't get the appropriate classes when the menu item is selected. –  El Yobo Aug 30 '11 at 3:26

Yes, that is implemented in 3.1, you have to make sure that the arguments passed to register_post_type have the has_archive flag set to true. See the Codex page about it.

You might also need to visit your permalink settings page and resubmit them, that seems to help things often.

share|improve this answer
    
Thanks to you Simon as well, much appreciated! –  Osu Feb 28 '11 at 15:47

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.