简单的说明下. 在后台 新建页面 的时候, 有个页面属性 - 模板, 想必大家都知道这是什么功能. 但是页面可能是多样化..比如: 很多时候..可能不同 页面, 需要不同的定制, 所有我考虑到这个. 就想出了一个办法. 就是 当我选择 模板?的时候就让后台的 项目自动添
简单的说明下.
在后台 新建页面 的时候, 有个页面属性 -> 模板, 想必大家都知道这是什么功能.
但是页面可能是多样化..比如:
很多时候..可能不同 页面, 需要不同的定制, 所有我考虑到这个. 就想出了一个办法. 就是 当我选择 模板?的时候就让后台的 项目自动添加需要的 模块
比如我选择了一个 茶园页面模板 就刷出 一个本类介绍模块?.
好了..直接上代码吧.
首先要后台加载 javascript select事件?触发重新提交URL. 这一步要写个 js 后台的时候导入:
define('MTHEME_JS', get_template_directory_uri() . '/js' );
function admin_enqueue_scripts(){
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'template', MTHEME_JS . '/admin/template.js', 'jquery', COAUTHORS_PLUS_VERSION, true);
}
add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts' );
?tempate.js:
jQuery(document).ready(function($) {
if($('#page_template').length > 0){
var uri = new Uri(window.location.href);
var val = uri.getQueryParamValue('temp');
$('#page_template').on('change', function(){
var v = $(this).val();
if(v != 'default'){
v = v.split('/');
//v = encodeURIComponent(v);
//v = URLencode(v);
if(uri.getQueryParamValue('temp')){
window.location.href = uri.replaceQueryParam('temp', v[0]+"_"+v[1]);
}else{
window.location.href = uri.addQueryParam('temp', v[0]+"_"+v[1]);
}
}
});
if(val){
$('#page_template').val(val.replace(/_/g, '/'));
}
}
});
这样给 选择模板 下拉框绑定了事件, ?当你选择的时候 模板 的时候.会发现页面重定向了 多了一个参数?&temp=XXXX.php?那我们在?functions.php?里就可以写入判断了. 下面是完整的 PHP
array(),
// 茶人页面模板
'page-templates/teamasters-page.php' => array(
'method'=> 'teamasters',
'content'=> '
头部本页介绍
这里可以加入幻灯片
茶人语录
名称
标题
上传图片
内容',
'element' => array(
"description" => array (
"name" => "teagarden_information",
"std" => "这里填默认的网页描述",
"title" => "本类介绍:"
)
)
),
// 茶园页面模板
'page-templates/teagarden-page.php' => array(
'method'=> 'teagarden',
'content'=> '',
'element' => array(
"description" => array (
'name' => "teagarden_information",
'std' => "这里填默认的网页描述",
'title' => "本类介绍:",
'type'=> 'editor'
)
)
),
// 茶礼页面模板
'page-templates/teaceremony-page.php' => array(),
// 茶道列表模板
'page-templates/teataoism-page.php' => array(),
// 茶道总览模板
'page-templates/teataoismtotal-page.php' => array(),
// 茶馆列表模板
'page-templates/teahouses-page.php' => array(),
// 茶馆总览模板
'page-templates/teahouseoverview-page.php' => array(),
);
$temp_name = str_replace ( "_", "/", $_GET ['temp'] );
// 判断是什么类型的
function return_tepmlate($temp_name) {
global $current_screen, $temp_name, $_meta_boxes, $temp_css;
foreach ($_meta_boxes as $k => $_box){
if($k == $temp_name){
return $_box['content'];
}
}
}
function custom_editor_content($content) {
global $current_screen, $temp_name, $temp_css;
$content = return_tepmlate ( $temp_name );
return $content;
}
function custom_editor_style() {
global $current_screen, $temp_css;
add_editor_style ( array (
'editor-style.css',
'editor-style.css'
) );
}
//返回自定义值
function _return_defined($defined){
global $temp_name, $_meta_boxes;
foreach ($_meta_boxes as $k => $_box){
if($k == $temp_name){
return $_box[$defined];
}
}
}
add_filter ( 'default_content', 'custom_editor_content' );
add_action ( 'admin_head', 'custom_editor_style' );
if ($_GET ['post'] && ! $_GET ['temp']) {
$post = get_page ( $_GET ['post'] );
$temp_name = $post->page_template;
$method = _return_defined('method');
if($method){
$method();
}
} elseif ($_GET ['post'] && $_GET ['action'] == 'edit') {
$method = _return_defined('method');
if($method){
$method();
}
} elseif ($_GET ['post_type'] == 'page'){
$method = _return_defined('method');
if($method){
$method();
}
}
if ($_POST) {
add_action ( 'save_post', 'save_postdata' );
}
}
function teamasters(){
}
// 茶园
function teagarden() {
global $temp_name, $_meta_boxes;
if(!_return_defined('element')){
return '';
}
// 在'add_meta_boxes'挂载 add_xz_box 函数
add_action ( 'add_meta_boxes', 'add_heads_box' );
function add_heads_box() {
add_meta_box ( 'head_box', '本类介绍 - 文章头部内容', 'head_box', 'page', 'advanced', 'core' );
}
function head_box($post) { // 显示设置区域的回调函数
global $temp_name, $_meta_boxes;
$description = _return_defined('element');
if($description){
foreach ( $description as $meta_box ) {
$meta_box_value = get_post_meta ( $post->ID, $meta_box ['name'] . '_value', true );
if ($meta_box_value == "")
$meta_box_value = $meta_box ['std'];
if($meta_box ['title']){
echo '
' . $meta_box ['title'] . '';
}
switch ($meta_box['type']){
case 'textarea':
echo ''.$meta_box_value.'';
break;
case 'text':
echo '';
break;
case 'editor':
wp_editor ( str_replace ( '\"', '"', $meta_box_value ), $meta_box ['name'] . '_value', $settings = array (
quicktags => 1,
tinymce => 1,
media_buttons => 0,
textarea_rows => 4
) );
break;
}
echo '';
// 自定义字段标题
//echo '
' . $meta_box ['title'] . '';
// 自定义字段输入框
// echo ''.$meta_box_value.'
';
}
}
}
}
// 保存数据
function save_postdata($post_id) {
global $post, $_meta_boxes, $temp_name;
if($_meta_boxes){
foreach ($_meta_boxes as $key => $value){
if($key == $post->page_template){
if($value['element']){
foreach ( $value['element'] as $meta_box ) {
if (! wp_verify_nonce ( $_POST [$meta_box ['name'] . '_noncename'], plugin_basename ( __FILE__ ) )) {
return $post_id;
}
if ('page' == $_POST ['post_type']) {
if (! current_user_can ( 'edit_page', $post_id ))
return $post_id;
} else {
if (! current_user_can ( 'edit_post', $post_id ))
return $post_id;
}
$data = $_POST [$meta_box ['name'] . '_value'];
if (get_post_meta ( $post_id, $meta_box ['name'] . '_value' ) == "")
add_post_meta ( $post_id, $meta_box ['name'] . '_value', $data, true );
elseif ($data != get_post_meta ( $post_id, $meta_box ['name'] . '_value', true ))
update_post_meta ( $post_id, $meta_box ['name'] . '_value', $data );
elseif ($data == "")
delete_post_meta ( $post_id, $meta_box ['name'] . '_value', get_post_meta ( $post_id, $meta_box ['name'] . '_value', true ) );
}
}
}
}
}
}
接下来就完事了…..我只是做了一个 例子, 希望大家给点意见或者更好的办法, 也希望我的办法能解决大家和我一样的情况….
忘了说:这只支持3.5版本, 要支持 3.4 需要改这里:
//因为3.5 里面get_page 可以返回 模板的值
$temp_name = $post->page_template;
//而且 3.4 里面并没有所以要用其他函数来获取.
$temp_name = get_post_meta( $post->ID, '_wp_page_template', true );
如上一改就行了~~~~