本人一直在用一个分类和文章模板选择器的,不记得是从哪里弄来的,就想拿出来分享一下。利用这个模板选择器你可以给每个分类和其下的文章指定模板,非常方便,制作模板也很简单,只要加上注释代码即可。下面是图片:

要实现此功能, 你需要在你的主题根目录文件夹下新建一个 template.php, 并将下面的代码复制进去 (或者直接下载此文件:

template.zip

):

  1. <?php
  2. function cat_temp_menus() {
  3. 	add_menu_page('模板选择器', '模板选择器', 'manage_options', basename(__FILE__), 'cat_temp_options_page');
  4. }
  5. function cat_temp_meta() {
  6. 	global $wpdb,$post_ID;
  7. 	$templates = cat_temp_get_page_templates();
  8. 	$current = cat_temp_post_template($post_ID);
  9. 	$out = '<select name="cat_temp_template" style="width:100%">';
  10. 	$out .= '<option value="none"';
  11. 	if ($post_ID == 0 ||!$current) $out .= ' selected="selected"';
  12. 	$out .= '> 请选择</option>';
  13. 	$out .= '<option value="/single.php"';
  14. 	if ($current == "/single.php") $out .= ' selected="selected"';
  15. 	$out .= '> 默认模板</option>';
  16. 	foreach ($templates as $template =>$file) {
  17. 		$out .= '<option value="'.$file.'"';
  18. 		if ($current == $file) $out .= ' selected="selected"';
  19. 		$out .= '>'.$template.'</option>';
  20. 	}
  21. 	$out .= "</select>";
  22. 	$out .= "<p> 选择文章模板</p>";
  23. 	echo $out;
  24. }
  25. function cat_temp_post_submit($post_ID) {
  26. 	global $wpdb;
  27. 	if ($_POST['cat_temp_template']) {
  28. 		$templates =  (get_option("cat_temp_post"));
  29. 		if ($_POST['cat_temp_template'] != 'none') {
  30. 			$templates[$post_ID] = $_POST['cat_temp_template'];
  31. 		}else {
  32. 			if ($templates[$post_ID]) {
  33. 				unset($templates[$post_ID]);
  34. 			}
  35. 		}
  36. 		update_option("cat_temp_post",($templates));
  37. 	}
  38. }
  39. function cat_temp_post_template($ID) {
  40. 	$templates =  (get_option("cat_temp_post"));
  41. 	return $templates[$ID];
  42. }
  43. function cat_temp($template) {
  44. 	global $wp_query;
  45. 	$post_obj = $wp_query->get_queried_object();
  46. 	if (is_single()) {
  47. 		$data = cat_temp_get_data();
  48. 		$categories = get_the_category($post_obj->ID);
  49. 	}else if (is_category()) {
  50. 		$data = cat_temp_get_data(true);
  51. 		$categories[0] = &get_category($post_obj->cat_ID);
  52. 	}
  53. 	$temp_data;
  54. 	foreach((array)$categories as $category) {
  55. 		if ($data[$category->term_id]['template'] != '0') {
  56. 			$temp_data[$data[$category->term_id]['template']] = $data[$category->term_id]['priority']+($category->term_id/80000);
  57. 		}
  58. 	}
  59. 	foreach((array)$data as $key =>$cat) {
  60. 		if ($cat['all'] == "all"&&$cat['template'] != "0") {
  61. 			$id = (is_single()) ?(int)$cat['id'] : $key;
  62. 			$descendants = get_term_children($id,'category');
  63. 			if ($descendants &&in_category($descendants)) {
  64. 				$temp_data[$cat['template']] = $cat['priority']+($cat['id']/80000);
  65. 			}
  66. 		}
  67. 	}
  68. 	if (is_array($temp_data)) {
  69. 		asort($temp_data);
  70. 		$template = array_shift(array_keys($temp_data));
  71. 	}
  72. 	if (is_single()) {
  73. 		$overRule = cat_temp_post_template($post_obj->ID);
  74. 		if ($overRule) $template = $overRule;
  75. 	}
  76. 	if (!empty($template)) {
  77. 		if (file_exists(TEMPLATEPATH.$template)) {
  78. 			include(TEMPLATEPATH.$template);
  79. 			exit;
  80. 		}
  81. 	}
  82. }
  83. function cat_temp_is_cat($cat,$_post = null) {
  84. 	if (in_category($cat,$_post)) {
  85. 		return true;
  86. 	}else {
  87. 		$descendants = get_term_children((int)$cat,'category');
  88. 		if ($descendants &&in_category($descendants,$_post)) return true;
  89. 	}
  90. 	return false;
  91. }
  92. function cat_temp_get_page_templates($str = "Template Name") {
  93. 	$themes = get_themes();
  94. 	$theme = get_current_theme();
  95. 	$templates = $themes[$theme]['Template Files'];
  96. 	$page_templates = array();
  97. 	if (is_array($templates)) {
  98. 		foreach((array)$templates as $template) {
  99. 			if (!file_exists($template)) $template = WP_CONTENT_DIR.$template;
  100. 			$template_data = implode('',file($template));
  101. 			$name = '';
  102. 			if (preg_match('|'.$str.':(.*)$|mi',$template_data,$name))
  103. 				$name = $name[1];
  104. 			if (!empty($name)) {
  105. 				$page_templates[trim($name)] = str_replace($themes[$theme]['Template Dir'],"",$template);
  106. 			}
  107. 		}
  108. 	}
  109. 	return $page_templates;
  110. }
  111. function cat_temp_cats($item,$current,$archive=false) {
  112. 	if ($archive) {
  113. 		$templates = cat_temp_get_page_templates('Archive Template');
  114. 		$default = '/archive.php';
  115. 	}else {
  116. 		$templates = cat_temp_get_page_templates();
  117. 		$default = '/single.php';
  118. 	}
  119. 	$out = '<select title="Template"  name="data';
  120. 	if ($archive) $out .= '[archive]';
  121. 	$out .= '['.$item.'][template]">';
  122. 	$out .= '<option value="0"';
  123. 	if ($current == "0") $out .= ' selected="selected"';
  124. 	$out .= '> 未指定</option>';
  125. 	$out .= '<option value="'.$default.'"';
  126. 	if ($current == $default) $out .= ' selected="selected"';
  127. 	$out .= '> 默认模板</option>';
  128. 	foreach ($templates as $template =>$file) {
  129. 		$out .= '<option value="'.$file.'"';
  130. 		if ($current == $file) $out .= ' selected="selected"';
  131. 		$out .= '>'.$template.'</option>';
  132. 	}
  133. 	$out .= "</select>";
  134. 	return $out;
  135. }
  136. function cat_temp_categories($child = 0) {
  137. 	$data = array(
  138. 		"hide_empty"=>false,
  139. 		"child_of"=>$child,
  140. 		"pad_count"=>false,
  141. 		);
  142. 	$categories = get_categories($data);
  143. 	$list = array();
  144. 	foreach ((array)$categories as $cat) {
  145. 		if ($cat->parent == $child) {
  146. 			$list[] = array(
  147. 				"name"=>$cat->name,
  148. 				"id"=>$cat->cat_ID,
  149. 				"count"=>cat_temp_getALLposts($cat->cat_ID),
  150. 				"acount"=>$cat->category_count,
  151. 				"child"=>cat_temp_categories($cat->cat_ID),
  152. 				);
  153. 		}
  154. 	}
  155. 	return $list;
  156. }
  157. function cat_temp_li_fun($data) {
  158. 	$out = "<ul class="cat-temp">";
  159. 	foreach ((array)$data as $root) {
  160. 		$out .= "<li>".$root['name']." (".$root['count'].")</li>";
  161. 		if (count($root['child']) >0) {
  162. 			$out .= cat_temp_li_fun($root['child']);
  163. 		}
  164. 	}
  165. 	$out .= "</ul>";
  166. 	return $out;
  167. }
  168. function cat_temp_priority($item,$current,$archive) {
  169. 	$pri = array("最低","低","中等","高","最高");
  170. 	$out = '<select  title="Template Priority" name="data';
  171. 	if ($archive) $out .= '[archive]';
  172. 	$out .= '['.$item.'][priority]">';
  173. 	$t = 0;
  174. 	for ($i = 10;$i >= 1;$i = $i-2) {
  175. 		$out .= '<option value="'.$i.'"';
  176. 		if (intval($current) == $i) $out .= ' selected="selected"';
  177. 		$out .= '>'.$pri[$t].'</option>';
  178. 		$t++;
  179. 	}
  180. 	$out .= "</select>";
  181. 	return $out;
  182. }
  183. function cat_temp_getALLposts($ID) {
  184. 	$td = array(
  185. 		'numberposts'=>-1,
  186. 		'category'=>$ID,
  187. 		);
  188. 	return count(get_posts($td));
  189. }
  190. function cat_temp_get_data($archive=false,$id=false) {
  191. 	$t = (!$archive) ?(get_option('cat_temp_data')) :  (get_option('cat_arch_data'));
  192. 	return (!$id) ?$t : $t[$id];
  193. }
  194. function cat_temp_update($data) {
  195. 	$archive = $data['archive'];
  196. 	unset($data['archive']);
  197. 	update_option('cat_temp_data',($data));
  198. 	update_option('cat_arch_data',($archive));
  199. }
  200. function cat_temp_delete() {
  201. 	delete_option('cat_temp_data');
  202. 	delete_option('cat_temp_post');
  203. 	delete_option('cat_arch_data');
  204. }
  205. function cat_temp_sub_cats($id,$data,$archive=false) {
  206. 	$out .= ' <input type="checkbox"';
  207. 	if ($data == "all") $out .= ' checked="checked"';
  208. 	$out .= ' name="data';
  209. 	if ($archive) $out .= "[archive]";
  210. 	$out .= '['.$id.'][all]" value="all" title="Apply to sub-categories" /> <small> 用于子分类</small>';
  211. 	return $out;
  212. }
  213. function cat_temp_templates($id,$archive) {
  214. 	if ($archive) {
  215. 		$title = "分类";
  216. 		$class = " class="noborder"";
  217. 		$class2 = " noborder";
  218. 	}else {
  219. 		$title = "文章";
  220. 	}
  221. 	$data = cat_temp_get_data($archive,$id);
  222. 	$out .= "<td class="r$class2" >$title:</td>";
  223. 	$out .= "<td $class><div>".cat_temp_cats($id,$data['template'],$archive);
  224. 	$out .= cat_temp_sub_cats($id,$data['all'],$archive).'</div></td>';
  225. 	$out .= "<td $class><div>".cat_temp_priority($id,$data['priority'],$archive)."</div></td>";
  226. 	return $out;
  227. }
  228. function cat_temp_td_fun($data,$padding = 5) {
  229. 	$out = "";
  230. 	foreach ((array)$data as $root) {
  231. 		$out .= '<tr>';
  232. 		$out .= '<td  rowspan="2">'.$root['id'];
  233. 		$out .= '<input type="hidden" name="data['.$root['id'].'][id]" value="'.$root['id'].'" />';
  234. 		$out .= '</td>';
  235. 		$out .= '<td  style="padding-left:'.$padding.'px;" rowspan="2">'.$root['name'].'</td>';
  236. 		$out .= cat_temp_templates($root['id'],true);
  237. 		$out .= '<td rowspan="2">'.$root['acount'].' ('.$root['count'].')</td>';
  238. 		$out .= "</tr><tr>";
  239. 		$out .= cat_temp_templates($root['id'],false);
  240. 		$out .= '</tr>';
  241. 		if (count($root['child']) >0) {
  242. 			$out .= cat_temp_td_fun($root['child'],$padding+10);
  243. 		}
  244. 	}
  245. 	return $out;
  246. }
  247. function cat_temp_options_page() {
  248. 	$_GET['lang'] = 'all';
  249. 	;echo '
  250. 	<div >
  251. 		<div  id="icon-themes"><br/>
  252. 		</div>
  253. 		<h2> 模板选择器</h2>
  254. 		';
  255. 		if ($_POST['update_theme']) {
  256. 			cat_temp_update($_POST['data']);
  257. 			echo '<div id="message" ><p> 成功保存.</p></div>';
  258. 		}
  259. 		;echo '  <p> 请选择分类和页面模板</p>
  260. 		<form method="post" action="';echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];;echo '">
  261. 			';
  262. 			if(function_exists('settings_fields')){
  263. 				settings_fields('cat-temp-options');
  264. 			}else {
  265. 				wp_nonce_field('update-options');
  266. 				;echo '    <input type="hidden" name="action" value="update" />
  267. 				';
  268. 			}
  269. 			;echo '    <table>
  270. 			<tr>
  271. 				<td style="vertical-align:top" width="70%"><table width="100%"  id="cat_temps">
  272. 					<thead>
  273. 						<tr>
  274. 							<th width="2%" >ID</th>
  275. 							<th> 分类</th>
  276. 							<th colspan="2"> 模板</th>
  277. 							<th width="4%"> 等级</th>
  278. 							<th> 文章数</th>
  279. 						</tr>
  280. 					</thead>
  281. 					<tbody>
  282. 						';echo cat_temp_td_fun(cat_temp_categories());;echo '            </tbody>
  283. 					</table></td>
  284. 					<td style="vertical-align:top"><table width="100%" >
  285. 						<thead>
  286. 							<tr>
  287. 								<th> 说明</th>
  288. 							</tr>
  289. 							<tr>
  290. 								<td> 按照以下说明制作模板:</td>
  291. 							</tr>
  292. 							<tr>
  293. 								<td>
  294. 									分类模板: