CPT 模板未用于发布

问题描述 投票:0回答:0

我创建了一个插件并向该插件添加了自定义帖子类型。然后我使用层次结构 single-{custom-post-type}.php 创建了一个模板。所以我的 cpt slug 是 wppro-events,单个帖子模板称为 single-wppro-events.php。我还在模板文件的顶部添加了下面的评论部分,但我的帖子模板没有用于 cpt。知道为什么不使用它。编辑页面上的页面属性也没有显示更改模板的选项。 Astra 是我正在使用的主题。知道为什么这不起作用吗?

/*
Template Name: Layout One
Template Post Type: wppro-events
*/

我也试过下面的代码,但还是不行:

/* Filter the single_template with our custom function*/
add_filter('single_template', 'my_custom_template');

function my_custom_template($single) {

    global $post;

    /* Checks for single template by post type */
    if ( $post->post_type == 'Event Items' ) {
        if ( file_exists( PLUGIN_PATH . '/single-wppro-events.php' ) ) {
            return PLUGIN_PATH . '/single-wppro-events.php';
        }
    }

    return $single;

}
wordpress custom-post-type wordpress-plugin-creation
© www.soinside.com 2019 - 2024. All rights reserved.