WordPress.org

Ready to get started?Download WordPress

Forums

Template Hierarchy For Custom Post Types / Page Types (3 posts)

  1. thebiganswer
    Member
    Posted 3 years ago #

    Hi All,

    On another forum someone wrote:

    "WordPress has incorporated custom post types into the template hierarchy and you have access to them at single-$posttype.php and archive-$posttype.php."

    If true, in which version has this been (or will be) implemented?

    Thanks.

  2. thebiganswer
    Member
    Posted 3 years ago #

    (In response to my own post, it is true of version 3.0.4 at least - my problem was correctly understanding the template naming convention)

    Ok, after some head scratching trying to get my custom post type permalinks to resolve to the corresponding template file rather than the 404 error page or otherwise, here's a few notes for anyone having a similar issue:

    Example
    So you've created/registered your custom type in your functions.php file, e.g.:

    `add_action('init', 'create_music_event');
    function create_music_event() {
    $music_event_args = array(

    'labels' => array(
    'name' => __( 'Music Event' ),
    'singular_name' => __( 'Music Event' )
    ),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'page',
    'hierarchical' => true,
    'rewrite' => array('slug' => 'music-events'),
    'description' => 'Music events',
    'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'page-attributes')
    );
    register_post_type('music_event',$music_event_args);
    }`

    And you've created some 'music event' posts from your admin panel. But which template will these 'music event' posts use when rendering the page? Well, if you copy your 'single.php' file, rename the copy 'single-music_event.php' then follow the link to you music event post, you should see your music event post.

    Verify
    You can check it's using your custom template file by inserting some text, e.g. <h1>Look I'm using the custom template file</h1>, in the single-music_event.php file.

    What's the point of all this?
    Well if you want to use custom types, and you want your custom type 'music_event' posts (for example) to have a different look and feel to your standard/other posts and pages, then custom templates are ideal.

    Other Issues
    Another issue you may find when working with custom templates is getting 404 error due to conflicting (i.e. identical) slug names. Without going into any detail (there are plenty of Google results) you may have created a Page which shares the same slug value as your new custom post type. For example, let's create a new Page called 'Music Events' and see that WordPress uses '.../music-events' as the slug. Well now, our custom type (see example above) also uses 'music-events' as the slug value. WordPress won't like this and you'll likely see a 404 error and/or whatever your 404.php template tells you.

  3. videogold
    Member
    Posted 2 years ago #

    Thanks, that's exactly what I needed!

    single-[custom_post_name].php

    archive-[custom_post_name].php

    Now that I look, it has it right in the template hierarchy in the codex, but I never understood what it meant.

    http://codex.wordpress.org/File:Template_Hierarchy.png

Topic Closed

This topic has been closed to new replies.

About this Topic