-4

This question already has an answer here:

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 created the single-memberPost.php but I can't get the theme to use this custom post template.

marked as duplicate by bueltge, Pieter Goosen, Mayeenul Islam, Howdy_McGee, shea Jul 20 '16 at 3:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please make use of our site search before asking duplicate questions – Pieter Goosen Jun 13 '16 at 13:08
1

File should be single-member_post.php instead of single-memberPost.php.

In single-{posttype} , {post_type} is the $post_type argument of the register_post_type() function.

Never use flush_rewrite_rules(); in init use it only on theme/plugin deactivate or activate.

Since this is a theme you can use it on after_switch_theme hook.

add_action( 'init', 'my_cpt_init' );
function register_cpt_member_post() {
    register_post_type( ... );
}

function my_rewrite_flush() {
    register_cpt_member_post();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
  • Custom post types should not be registered in themes, it should be done in a plugin – Pieter Goosen Jun 13 '16 at 13:09
  • @PieterGoosen , yes. – bravokeyl Jun 13 '16 at 13:10
0

Your post type (member_post) doesn't match your template slug (memberPost). Whichever you choose, it must be all lower case.

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