CorePress使用插件的方式添加文章和頁面模板
網(wǎng)上的方法都比較麻煩,整理了一下代碼,只需要替換頁面名稱和頁面路徑即可
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變量里面的參數(shù)即可,slug為文件名稱,name為模板名稱,path為文件路徑。多個模板,多添加幾個array即可。
另外用的時候,建議也改一改常量CP_PLUGIN_DIR,改成唯一的名字。
版權(quán)聲明:
作者:applek
鏈接:http://www.yydfqli.cn/corepresssybjdfstjwzhymna.html
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END