Home » WP使用, 进阶使用 » 主题制作小技巧之二:自定义categories列表

主题制作小技巧之二:自定义categories列表

和“自定义Page列表”的思路是一样的,wp自带的函数wp_list_categories的使用方法你可以看这里。今天就不多废话了:
一,打开你主题的functions.php文件,在<?php …?>中添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Displays a list of categories
function pj_list_categories($exclude='', $limit=0) {
	if (strlen($exclude)>0) $exclude = '&exclude=' . $exclude;
	$categories = get_categories('hide_empty=1'.$exclude);
	$first = true; $count = 0;
	foreach ($categories as $category) {
		$count++; if ($count>$limit && $limit>0) break;
		if ($category->parent<1) {
			if ($first) { $first = false; $f = ' '; } else { $f = ''; }
			?><?php echo $f; ?>
			<li><a class="CategoryA" href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?><?php echo $raquo; ?></a></li>
			<?php
		}
	}
}

同样注意:

1
<li><a class="CategoryA" href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?><?php echo $raquo; ?></a></li>

这里除<?php ?>里的内容外你都可以修改!
二,然后在你想调用分类列表的地方通过函数pj_list_categories来实现

1
<?php pj_list_categories();?>

Leave a comment