将自定义主题模板添加到自定义帖子类型

时间:2016-06-13 作者:KKBSE2016

我创建了一个名为MemberPost的新自定义帖子类型,并希望它遵循与我的主索引略有不同的模板(主新闻保存的地方)。我已经创建了一个自定义帖子类型,并将其插入我的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();
}
然后我创建了一个memberPost。php,但我无法获得使用此自定义帖子模板的主题。

2 个回复
最合适的回答,由SO网友:bravokeyl 整理而成

文件应为single-member_post.php 而不是single-memberPost.php.

在里面single-{posttype} , {post_type}$post_type 的参数register_post_type() 作用

从不使用flush_rewrite_rules(); 在里面init 仅在主题/插件停用或激活时使用。

因为这是一个主题,你可以在上面使用它after_switch_theme

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\' );

SO网友:cjbj

您的帖子类型(member_post) 与模板slug不匹配(memberPost). 无论你选择哪个,它都必须是小写的。

相关推荐