欢迎光临
我们一直在努力

WordPress 分类及分类下的文章添加模板选择功能

建站超值云服务器,限时71元/月

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

WordPress 分类及分类下的文章添加模板选择功能

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

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

<?php function cat_temp_menus() { add_menu_page(‘模板选择器’, ‘模板选择器’, ‘manage_options’, basename(__FILE__), ‘cat_temp_options_page’); } function cat_temp_meta() { global $wpdb,$post_ID; $templates = cat_temp_get_page_templates(); $current = cat_temp_post_template($post_ID); $out = ‘<select name=”cat_temp_template” style=”width:100%”>’; $out .= ‘<option value=”none”‘; if ($post_ID == 0 ||!$current) $out .= ‘ selected=”selected”‘; $out .= ‘>请选择</option>’; $out .= ‘<option value=”/single.php”‘; if ($current == “/single.php”) $out .= ‘ selected=”selected”‘; $out .= ‘>默认模板</option>’; foreach ($templates as $template =>$file) { $out .= ‘<option value=”‘.$file.'”‘; if ($current == $file) $out .= ‘ selected=”selected”‘; $out .= ‘>’.$template.'</option>’; } $out .= “</select>”; $out .= “<p>选择文章模板</p>”; echo $out; } function cat_temp_post_submit($post_ID) { global $wpdb; if ($_POST[‘cat_temp_template’]) { $templates = (get_option(“cat_temp_post”)); if ($_POST[‘cat_temp_template’] != ‘none’) { $templates[$post_ID] = $_POST[‘cat_temp_template’]; }else { if ($templates[$post_ID]) { unset($templates[$post_ID]); } } update_option(“cat_temp_post”,($templates)); } } function cat_temp_post_template($ID) { $templates = (get_option(“cat_temp_post”)); return $templates[$ID]; } function cat_temp($template) { global $wp_query; $post_obj = $wp_query->get_queried_object(); if (is_single()) { $data = cat_temp_get_data(); $categories = get_the_category($post_obj->ID); }else if (is_category()) { $data = cat_temp_get_data(true); $categories[0] = &get_category($post_obj->cat_ID); } $temp_data; foreach((array)$categories as $category) { if ($data[$category->term_id][‘template’] != ‘0’) { $temp_data[$data[$category->term_id][‘template’]] = $data[$category->term_id][‘priority’]+($category->term_id/80000); } } foreach((array)$data as $key =>$cat) { if ($cat[‘all’] == “all”&&$cat[‘template’] != “0”) { $id = (is_single()) ?(int)$cat[‘id’] : $key; $descendants = get_term_children($id,’category’); if ($descendants &&in_category($descendants)) { $temp_data[$cat[‘template’]] = $cat[‘priority’]+($cat[‘id’]/80000); } } } if (is_array($temp_data)) { asort($temp_data); $template = array_shift(array_keys($temp_data)); } if (is_single()) { $overRule = cat_temp_post_template($post_obj->ID); if ($overRule) $template = $overRule; } if (!empty($template)) { if (file_exists(TEMPLATEPATH.$template)) { include(TEMPLATEPATH.$template); exit; } } } function cat_temp_is_cat($cat,$_post = null) { if (in_category($cat,$_post)) { return true; }else { $descendants = get_term_children((int)$cat,’category’); if ($descendants &&in_category($descendants,$_post)) return true; } return false; } function cat_temp_get_page_templates($str = “Template Name”) { $themes = get_themes(); $theme = get_current_theme(); $templates = $themes[$theme][‘Template Files’]; $page_templates = array(); if (is_array($templates)) { foreach((array)$templates as $template) { if (!file_exists($template)) $template = WP_CONTENT_DIR.$template; $template_data = implode(”,file($template)); $name = ”; if (preg_match(‘|’.$str.’:(.*)$|mi’,$template_data,$name)) $name = $name[1]; if (!empty($name)) { $page_templates[trim($name)] = str_replace($themes[$theme][‘Template Dir’],””,$template); } } } return $page_templates; } function cat_temp_cats($item,$current,$archive=false) { if ($archive) { $templates = cat_temp_get_page_templates(‘Archive Template’); $default = ‘/archive.php’; }else { $templates = cat_temp_get_page_templates(); $default = ‘/single.php’; } $out = ‘<select title=”Template” class=”ct_template” name=”data’; if ($archive) $out .= ‘[archive]’; $out .= ‘[‘.$item.’][template]”>’; $out .= ‘<option value=”0″‘; if ($current == “0”) $out .= ‘ selected=”selected”‘; $out .= ‘>未指定</option>’; $out .= ‘<option value=”‘.$default.'”‘; if ($current == $default) $out .= ‘ selected=”selected”‘; $out .= ‘>默认模板</option>’; foreach ($templates as $template =>$file) { $out .= ‘<option value=”‘.$file.'”‘; if ($current == $file) $out .= ‘ selected=”selected”‘; $out .= ‘>’.$template.'</option>’; } $out .= “</select>”; return $out; } function cat_temp_categories($child = 0) { $data = array( “hide_empty”=>false, “child_of”=>$child, “pad_count”=>false, ); $categories = get_categories($data); $list = array(); foreach ((array)$categories as $cat) { if ($cat->parent == $child) { $list[] = array( “name”=>$cat->name, “id”=>$cat->cat_ID, “count”=>cat_temp_getALLposts($cat->cat_ID), “acount”=>$cat->category_count, “child”=>cat_temp_categories($cat->cat_ID), ); } } return $list; } function cat_temp_li_fun($data) { $out = “<ul class=\”cat-temp\”>”; foreach ((array)$data as $root) { $out .= “<li>”.$root[‘name’].” (“.$root[‘count’].”)</li>”; if (count($root[‘child’]) >0) { $out .= cat_temp_li_fun($root[‘child’]); } } $out .= “</ul>”; return $out; } function cat_temp_priority($item,$current,$archive) { $pri = array(“最低”,”低”,”中等”,”高”,”最高”); $out = ‘<select class=”ct_priority” title=”Template Priority” name=”data’; if ($archive) $out .= ‘[archive]’; $out .= ‘[‘.$item.’][priority]”>’; $t = 0; for ($i = 10;$i >= 1;$i = $i-2) { $out .= ‘<option value=”‘.$i.'”‘; if (intval($current) == $i) $out .= ‘ selected=”selected”‘; $out .= ‘>’.$pri[$t].'</option>’; $t++; } $out .= “</select>”; return $out; } function cat_temp_getALLposts($ID) { $td = array( ‘numberposts’=>-1, ‘category’=>$ID, ); return count(get_posts($td)); } function cat_temp_get_data($archive=false,$id=false) { $t = (!$archive) ?(get_option(‘cat_temp_data’)) : (get_option(‘cat_arch_data’)); return (!$id) ?$t : $t[$id]; } function cat_temp_update($data) { $archive = $data[‘archive’]; unset($data[‘archive’]); update_option(‘cat_temp_data’,($data)); update_option(‘cat_arch_data’,($archive)); } function cat_temp_delete() { delete_option(‘cat_temp_data’); delete_option(‘cat_temp_post’); delete_option(‘cat_arch_data’); } function cat_temp_sub_cats($id,$data,$archive=false) { $out .= ‘ <input type=”checkbox”‘; if ($data == “all”) $out .= ‘ checked=”checked”‘; $out .= ‘ name=”data’; if ($archive) $out .= “[archive]”; $out .= ‘[‘.$id.’][all]” value=”all” title=”Apply to sub-categories” /> <small>用于子分类</small>’; return $out; } function cat_temp_templates($id,$archive) { if ($archive) { $title = “分类”; $class = ” class=\”noborder\””; $class2 = ” noborder”; }else { $title = “文章”; } $data = cat_temp_get_data($archive,$id); $out .= “<td class=\”r$class2\” >$title:</td>”; $out .= “<td $class><div>”.cat_temp_cats($id,$data[‘template’],$archive); $out .= cat_temp_sub_cats($id,$data[‘all’],$archive).'</div></td>’; $out .= “<td $class><div>”.cat_temp_priority($id,$data[‘priority’],$archive).”</div></td>”; return $out; } function cat_temp_td_fun($data,$padding = 5) { $out = “”; foreach ((array)$data as $root) { $out .= ‘<tr>’; $out .= ‘<td class=”c” rowspan=”2″>’.$root[‘id’]; $out .= ‘<input type=”hidden” name=”data[‘.$root[‘id’].’][id]” value=”‘.$root[‘id’].'” />’; $out .= ‘</td>’; $out .= ‘<td class=”wide” style=”padding-left:’.$padding.’px;” rowspan=”2″>’.$root[‘name’].'</td>’; $out .= cat_temp_templates($root[‘id’],true); $out .= ‘<td rowspan=”2″>’.$root[‘acount’].’ (‘.$root[‘count’].’)</td>’; $out .= “</tr><tr>”; $out .= cat_temp_templates($root[‘id’],false); $out .= ‘</tr>’; if (count($root[‘child’]) >0) { $out .= cat_temp_td_fun($root[‘child’],$padding+10); } } return $out; } function cat_temp_options_page() { $_GET[‘lang’] = ‘all’; ;echo ‘ <div class=”wrap cat-template”> <div class=”icon32″ id=”icon-themes”><br/> </div> <h2>模板选择器</h2> ‘; if ($_POST[‘update_theme’]) { cat_temp_update($_POST[‘data’]); echo ‘<div id=”message” class=”updated”><p>成功保存.</p></div>’; } ;echo ‘ <p>请选择分类和页面模板</p> <form method=”post” action=”‘;echo $_SERVER[‘PHP_SELF’].”?”.$_SERVER[‘QUERY_STRING’];;echo ‘”> ‘; if(function_exists(‘settings_fields’)){ settings_fields(‘cat-temp-options’); }else { wp_nonce_field(‘update-options’); ;echo ‘ <input type=”hidden” name=”action” value=”update” /> ‘; } ;echo ‘ <table> <tr> <td style=”vertical-align:top” width=”70%”><table width=”100%” class=”widefat” id=”cat_temps”> <thead> <tr> <th width=”2%” class=”c”>ID</th> <th>分类</th> <th colspan=”2″>模板</th> <th width=”4%”>等级</th> <th>文章数</th> </tr> </thead> <tbody> ‘;echo cat_temp_td_fun(cat_temp_categories());;echo ‘ </tbody> </table></td> <td style=”vertical-align:top”><table width=”100%” class=”widefat”> <thead> <tr> <th>说明</th> </tr> <tr> <td>按照以下说明制作模板:</td> </tr> <tr> <td> 分类模板: <pre style=”direction:ltr”> <?php /** Archive Template: 分类模板名 **/ ?> </pre> </td> </tr> <tr> <td> 文章模板: <pre style=”direction:ltr”> <?php /** Template Name: 文章模板名 **/ ?> </pre> </td> </tr> </thead> </table></td> </tr> </table> <p class=”submit”> <input type=”submit” class=”button-primary autowidth” name=”update_theme” value=”保存” /> </p> </form> </div> ‘; } add_action(‘admin_menu’,’cat_temp_menus’); add_action(‘save_post’,’cat_temp_post_submit’); add_filter(‘template_redirect’,’cat_temp’); ?>

1
include (TEMPLATEPATH . '/template.php'
赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WordPress 分类及分类下的文章添加模板选择功能
分享到: 更多 (0)