覆盖插件自定义帖子类型‘Single’视图

时间:2017-08-10 作者:SupaMonkey

我有一个主题,它使用自定义的帖子类型(“room”),注册在自己的插件中。

// add action to create room post type
add_action( \'init\', \'gdlr_create_room\' );
if( !function_exists(\'gdlr_create_room\') ){
    function gdlr_create_room() {
        global $theme_option;

        if( !empty($theme_option[\'room-slug\']) ){
            $room_slug = $theme_option[\'room-slug\'];
            $room_category_slug = $theme_option[\'room-category-slug\'];
            $room_tag_slug = $theme_option[\'room-tag-slug\'];
        }else{
            $room_slug = \'room\';
            $room_category_slug = \'room_category\';
            $room_tag_slug = \'room_tag\';
        }

        register_post_type( \'room\',
            array(
                \'labels\' => array(
                    \'name\'               => __(\'Rooms\', \'gdlr-hotel\'),
                    \'singular_name\'      => __(\'Room\', \'gdlr-hotel\'),
                    \'add_new\'            => __(\'Add New\', \'gdlr-hotel\'),
                    \'add_new_item\'       => __(\'Add New Room\', \'gdlr-hotel\'),
                    \'edit_item\'          => __(\'Edit Room\', \'gdlr-hotel\'),
                    \'new_item\'           => __(\'New Room\', \'gdlr-hotel\'),
                    \'all_items\'          => __(\'All Rooms\', \'gdlr-hotel\'),
                    \'view_item\'          => __(\'View Room\', \'gdlr-hotel\'),
                    \'search_items\'       => __(\'Search Room\', \'gdlr-hotel\'),
                    \'not_found\'          => __(\'No rooms found\', \'gdlr-hotel\'),
                    \'not_found_in_trash\' => __(\'No rooms found in Trash\', \'gdlr-hotel\'),
                    \'parent_item_colon\'  => \'\',
                    \'menu_name\'          => __(\'Rooms (Hotel)\', \'gdlr-hotel\')
                ),
                \'public\'             => true,
                \'publicly_queryable\' => true,
                \'show_ui\'            => true,
                \'show_in_menu\'       => true,
                \'query_var\'          => true,
                \'rewrite\'            => array( \'slug\' => $room_slug  ),
                \'capability_type\'    => \'post\',
                \'has_archive\'        => true,
                \'hierarchical\'       => false,
                \'menu_position\'      => 7,
                \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\' )
            )
        );

        // create room categories
        register_taxonomy(
            \'room_category\', array("room"), array(
                \'hierarchical\' => true,
                \'show_admin_column\' => true,
                \'label\' => __(\'Room Categories\', \'gdlr-hotel\'), 
                \'singular_label\' => __(\'Room Category\', \'gdlr-hotel\'), 
                \'rewrite\' => array( \'slug\' => $room_category_slug  )));
        register_taxonomy_for_object_type(\'room_category\', \'room\');

        // create custom taxonomy for room category
        if( is_admin() && class_exists(\'gdlr_tax_meta\') ){
            global $gdlr_sidebar_controller;

            new gdlr_tax_meta( 
                array(
                    \'taxonomy\'=>\'room_category\',
                    \'slug\'=>\'gdlr_hotel_branch\'
                ),
                array(
                    \'upload\' => array(
                        \'title\'=> __(\'Hotel Thumbnail\', \'gdlr-song\'),
                        \'type\'=> \'upload\'
                    ),
                    \'content\' => array(
                        \'title\'=> __(\'Hotel Location\', \'gdlr-song\'),
                        \'type\'=> \'textarea\'
                    )
                )
            );
        }


        // create room tag
        register_taxonomy(
            \'room_tag\', array(\'room\'), array(
                \'hierarchical\' => false, 
                \'show_admin_column\' => true,
                \'label\' => __(\'Room Tags\', \'gdlr-hotel\'), 
                \'singular_label\' => __(\'Room Tag\', \'gdlr-hotel\'),  
                \'rewrite\' => array( \'slug\' => $room_tag_slug  )));
        register_taxonomy_for_object_type(\'room_tag\', \'room\');  

        // add filter to style single template
        add_filter(\'single_template\', \'gdlr_register_room_template\');
    }
}

if( !function_exists(\'gdlr_register_room_template\') ){
    function gdlr_register_room_template($single_template) {
        global $post;

        if ($post->post_type == \'room\') {
            $single_template = dirname(dirname( __FILE__ )) . \'/single-room.php\';
        }
        return $single_template;    
    }
}
我基本上只想跳过最后一点,这样就不用使用“dirname(dirname(FILE )) . \'/单人房。php’;“而是在我的子主题目录中使用我自己的模板,即:

$single_template = get_stylesheet_directory() .\'/single-room.php\';
如何重写它,使其使用上述文件?

edit: 这就是我在childs函数中的内容。php文件,但似乎没有工作(即使它似乎正在加载代码):

add_filter(\'single_template\',\'nobleman_rooms\');

function nobleman_rooms($single_template) {
    global $post;

    if ($post->post_type == \'room\') {
        $single_template = get_stylesheet_directory() .\'/single-room.php\';
    }
    return $single_template;    
}

1 个回复
最合适的回答,由SO网友:Chris Cox 整理而成

remove_filter(\'single_template\', \'gdlr_register_room_template\', 11);
这应该可以做到。原来的钩子没有指定优先级,因此它将默认为10,指定11应该确保它之后立即被删除。

然后,WP将恢复其默认行为,即在子主题的根中查找模板,然后在父主题的根中查找模板。

结束

相关推荐

Theme styling for plugins

我有一个插件,它有自己的CSS,用于在使用相关短代码时生成的内容。我正在尝试创建一个主题来重新设置我网站前端的样式,但由于这个插件有自己的CSS,我如何在新主题中修改它?