带有CPT的自定义模板不在前端显示

时间:2020-04-11 作者:Tarik

我创建了一个WP插件,它添加了一个CPT,允许用户选择自定义模板。

当我使用MVC design 但是当我搬到一个新的MVC style 它不会在前端输出,似乎也不会将自定义模板分配给页面!

我使用了以下挂钩:

    add_filter(
        \'theme_page_templates\', array( $this, \'add_new_template\' )
    );

    add_filter(
        \'quick_edit_dropdown_pages_args\',
        array( $this, \'register_project_templates\' )
    );

  add_filter(
    \'wp_insert_post_data\', 
    array( $this, \'register_project_templates\' ) 
);

            // Add your templates to this array.
            $this->templates = array(
                \'api.php\' => \'API\',
                \'print_invoice.php\' => \'Print Invoice\',
                \'wpc-post.php\' => \'custom Post\',
                \'dashboard.php\' => \'Dashboard\',
            );

            // self::create_dashboard_page();

            add_action(\'add_meta_boxes\', array($this, \'meta_box_visibility\'));

            add_action(\'save_post\', array($this, \'wporg_save_postdata\'), 10, 2);

                    /* Filter the single_template with our custom function*/
                    add_filter( \'template_include\', array($this, \'my_plugin_templates\'));

            add_filter(
                \'manage_dashboard_posts_columns\',
                array($this, \'custom_post_type_columns\')
            );

            add_action(
                \'manage_dashboard_posts_custom_column\',
                array($this, \'custom_column_content\')
            );

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

使用以下代码,我能够加载自定义模板。

public function register_hook_callbacks(){
                    // Enqueue Styles & Scripts.
                    add_action( \'wp_enqueue_scripts\', 
                    array( $this, \'enqueue_scripts\' ) );

add_filter(\'template_include\', 
    array( $this, \'view_project_template\') 
);
    }