CorePress使用插件的方式添加文章和页面模板

网上的方法都比较麻烦,整理了一下代码,只需要替换页面名称和页面路径即可

define('CP_PLUGIN_DIR', plugin_dir_path(__FILE__));
$cp_page_templates = array(
    array("slug" => "page-corepress.php",
        "name" => "CorePress-Pro主题页面",
        "path" => CP_PLUGIN_DIR . "/page-corepress.php",
    ),
);
add_filter('page_template', 'cp_page_template');
add_filter('single_template', 'cp_page_template');
function cp_page_template($page_template)
{
    global $cp_page_templates;
    $slug = get_page_template_slug();
    foreach ($cp_page_templates as $item) {
        if ($slug == $item['slug']) {
            $page_template = $item['path'];
        }
        return $page_template;
    }
}
add_filter( 'theme_page_templates', 'cp_page_add_template_to_select', 10, 4 );
add_filter( 'theme_post_templates', 'cp_page_add_template_to_select', 10, 4 );

function cp_page_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) {
    global $cp_page_templates;
    foreach ($cp_page_templates as $item) {
        $post_templates[$item['slug']] = $item['name'];
    }
    return $post_templates;
}

其中,只需要修改$cp_page_templates变量里面的参数即可,slug为文件名称,name为模板名称,path为文件路径。多个模板,多添加几个array即可。

另外用的时候,建议也改一改常量CP_PLUGIN_DIR,改成唯一的名字。

 

上一篇 WordPress如何识别Windows11评论
下一篇 Fancybox 和 lazyload 同时存在的问题
目录
applek

applek管理员

个人说明在个人中心里面设置

本月创作热力图

最新评论
chinacnd
chinacnd
6月26日
这个主题是怎么做到加载速度那么快的?
风起云涌
风起云涌
6月17日
前台投稿现在有点太抽象了 希望可以弄成那种分区填写的 标题,内容,图片,目录,标签,这样的
评论于留言建议
风起云涌
风起云涌
6月17日
希望加个开关去掉页脚的站点身份,还有自定义页脚居中文字
评论于留言建议
卡卡
卡卡
5月31日
倒是放个演示地址啊