0

I've created a custom post type and custom taxonomy (all fine so far!) but when I try to view the post it's giving me a page not found.

What do I need to do to setup a template which the custom post type below will use?

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'members',
        array(
            'labels' => array(
                'name' => __( 'Members' ),
                'singular_name' => __( 'Members' )
            ),
        'capability_type' => 'post',
        'supports' => array('title','editor','comments'),   
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'members' ),
        )
    );
}

function members_init() {
    // create a new taxonomy    
    register_taxonomy(
        'members',
        'members',
        array(
            'label' => __( 'Member Categories' ),
            'sort' => true,
            'hierarchical' => true,
            'args' => array( 'orderby' => 'term_order' ),
            'rewrite' => array( 'slug' => 'members' )
        )
    );  

}
add_action( 'init', 'members_init' );

1 Answer 1

2

According to the WordPress template hierarchy, a custom post type 'my-cpt' will first try to use single-my-cpt, then single.php and then index.php. The above issue is not caused by templates - indeed 404s are used when the content, not the template, cannot be found.

You are using a custom rewrite:

'rewrite' => array( 'slug' => 'members' )

For WordPress to recognise this you'll need to flush your rewrite rules by visiting the Settings > Permalink page.

2
  • Thanks, flushing the permalinks (I always forget to do that!!) worked. So if I create a template called single-members.php that should work? It's now showing a template just not sure what one!
    – Rob
    Commented Apr 4, 2012 at 11:16
  • 1
    If you create single-members.php it will use that, if not it will use single.php. In the unlikely case that doesn't exist either it will use index.php. Commented Apr 4, 2012 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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