WordPress子栏目模板继承

2

参考了老外的思路,改进了下代码,可以支持ID和别名判断.

用法:
category.php改名为category-default.php,新建一个category.php文件用来做入口,代码片段如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$catid = get_query_var('cat');
if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') )
	die(include( TEMPLATEPATH . '/category-' . $catid . '.php'));
$cat = &get_category($catid);
if ( file_exists(TEMPLATEPATH . '/category-' . $cat->slug . '.php') )
	die(include( TEMPLATEPATH . '/category-' . $cat->slug . '.php'));
$parent = $cat->category_parent;
while ($parent){
	$cat = &get_category($parent);
	if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') )
		die(include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php'));
	if ( file_exists(TEMPLATEPATH . '/category-' . $cat->slug . '.php') )
		die(include (TEMPLATEPATH . '/category-' . $cat->slug . '.php'));
}
include (TEMPLATEPATH . '/category-default.php');

共 2 条评论

  1. 回复

    code 插件 用的是什么? 效果挺不错的

    • 回复

      样式表是自己写的,什么插件都一样.

回复 海底de鱼 X

您的邮箱不会公开,当您的评论有新的回复时,会通过您填写的邮箱向您发送评论内容。 必填字段 *

为何看不到我发布的评论?

正在提交, 请稍候...