Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Samuel Glister
Samuel Glister
12,471 Points

Adding custom theme template to custom post type

I've created a new custom post type called MemberPost and want it to follow a slightly different template to my main index (which is where main news is kept). I've created a custom post type and inserted this into my functions.php.

    add_filter('excerpt_more', 'new_excerpt_more');

    add_action( 'init', 'register_cpt_member_post' );

    function register_cpt_member_post() {

    $labels = array(
        'name' => __( 'MemberPost', 'member-post' ),
        'singular_name' => __( 'MemberPost', 'member-post' ),
        'add_new' => __( 'Add New', 'member-post' ),
        'add_new_item' => __( 'Add New MemberPost', 'member-post' ),
        'edit_item' => __( 'Edit MemberPost', 'member-post' ),
        'new_item' => __( 'New MemberPost', 'member-post' ),
        'view_item' => __( 'View MemberPost', 'member-post' ),
        'search_items' => __( 'Search MemberPost', 'member-post' ),
        'not_found' => __( 'No memberpost found', 'member-post' ),
        'not_found_in_trash' => __( 'No memberpost found in Trash', 'member-post' ),
        'parent_item_colon' => __( 'Parent MemberPost:', 'member-post' ),
        'menu_name' => __( 'MemberPost', 'member-post' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Post containing the months member content',
        'supports' => array( 'editor', 'title', 'thumbnail'),
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array('slug' => 'member-post'),
        'capability_type' => 'post'
    );

    register_post_type( 'member_post', $args );
    flush_rewrite_rules();
    }

I then create the single-memberPost.php but I can't get the theme to use this custom post template.

Any help would be appreciated.

Thanks.

1 Answer

Samuel Glister
Samuel Glister
12,471 Points

Sorted it - the theme template should have been single-member_post.php not single-memberPost.php!