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've built a site with a custom post type called Testimonials and am using a custom template to display the posts edit: template name is page-testimonials.php. All is working as expected with the default permalink structure, however when I set the permalinks to /%postname%/ the custom template is ignored and the posts are showed on a blog archive....

Here's the code I've used to register the post type. Have I missed something required to make it use the proper template with custom permalinks?

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'testimonial',
        array(
            'labels' => array(
                'name' => __( 'Testimonials' ),
                'singular_name' => __( 'Testimonial' ),
                'add_new_item' => __('Add New Item'),
                'edit_item' => __('Edit Item'),
                'new_item' => __('New Item'),
                'view_item' => __('View Item'),    'add_new_item' => __('Add New Item'),
                'edit_item' => __('Edit Item'),
                'new_item' => __('New Item'),
                'view_item' => __('View Item')
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'testimonials',
            'show_in_nav_menus' => true,
            'capability_type' => 'page',
            'hierarchical' => true,
           'supports' => array( 'title', 'editor', 'thumbnail' ),
           'query_var' => true)
        )
    );
}
share|improve this question
1  
What is the name of your template? –  Joseph Nov 19 '12 at 2:39
    
the template is page-testimonials.php –  Anton Martin Nov 19 '12 at 4:39
add comment

2 Answers

up vote 1 down vote accepted

Custom post types have a different hierarchy than pages or posts.

They'll use single-{{post_type}}.php and fall back to single.php. Post type archives will use archive-{{post_type}}.php and fall back to archive.php.

Your question isn't super clear, but if you want your custom post type's singular page to use the page-testimonials.php template, rename it to single-testimonials.php.

I suspect, however, that you meant that page-testimonials.php was display a list of the testimonials. In which case, rename it to archive-testomonials.php and it should work fine. What happened before is you saw the permalink yoursite.com/?page_id={{some_number}}. Which works -- that's always going to be a page, but with the post type as you registered it (with 'has_archive' => true) yoursite.com/testimonials attempts to display the custom post type archive.

A final note: if you did any query modifications or secondary queries to get the testimonials post type in your page-testimonials.php template, you can remove those. WordPress will take care of making sure the correct items are available in the archive-{{post_type}}.php template.

You should also have a look at the template hierarchy

share|improve this answer
    
Awesome, well written and concise answer. You sir are a legend. To state the obvious that solved my question and gave me some great further insight into how wordpress operates. –  Anton Martin Nov 19 '12 at 9:44
add comment

i see an error:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'testimonial',
        array(
            'labels' => array(
                'name' => __( 'Testimonials' ),
            'singular_name' => __( 'Testimonial' ),
            'add_new_item' => __('Add New Item'),
            'edit_item' => __('Edit Item'),
            'new_item' => __('New Item'),
            'view_item' => __('View Item'),    'add_new_item' => __('Add New Item'),
            'edit_item' => __('Edit Item'),
            'new_item' => __('New Item'),
            'view_item' => __('View Item')
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'testimonials'),  **<---- miss )**
        'show_in_nav_menus' => true,
        'capability_type' => 'page',
        'hierarchical' => true,
       'supports' => array( 'title', 'editor', 'thumbnail' ),
       'query_var' => true)
    )
);

}

share|improve this answer
    
Please explain where –  Pieter Goosen 19 hours ago
    
before: 'rewrite' => array('slug' => 'testimonials', after: 'rewrite' => array('slug' => 'testimonials'), –  responsivelab 19 hours ago
    
You should edit your answer and add your comments to that. Always when you answer, give a short explanation to any code that you add. Code alone is meaningless, so are links –  Pieter Goosen 19 hours ago
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.