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管理员

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

本月创作热力图

最新评论
Mr.C
Mr.C
4月12日
个人建议,下个版本考虑将下载地址加密(防止采集)
Felix
Felix
4月1日
你好我想请教一下,为什么我php和redis都安装了,还是连接不上,一直显示未安装,旧插件文件也清理了
Mike
Mike
4月1日
当前页跳转一个页面,然后回退上一页,顶部进度条会缓慢加载,并且加载不完
评论于留言建议
Mike
Mike
4月1日
文章页划到最底部,侧边栏中切换作者发言会溢出
评论于留言建议
陇安辞
陇安辞
3月31日
guzhang
评论于关于本站