Tùy biến hiển thị taxonomy
Tùy biến hiển thị category/taxonomy:
* Hiển thị danh mục con có trong danh mục mẹ.
<ul class="category-class">
<?php
$catsy = get_the_category();
$myCat = $catsy->cat_ID;
wp_list_categories('orderby=id&child_of='.$myCat);
?>
</ul>
|
* Hiển thị chỉ danh mục chính loại trừ các danh mục con.
<?php
wp_list_categories('depth=1&title_li=&exclude=1&show_count=1&hide_empty=0');
?>
|
* Giả định rằng bạn muốn hiển thị category/subcategory (đa tầng) trong trang wordpress mà xác định từ category chính lấy từ post.
Hãy tham khảo đoạn code sau nó sẽ rất hữu ích đấy.
//# first of all, you need to get the category array,
// and I did it, putting the line bellow inside the loop
$category = get_the_category(get_the_ID());
// once you have the category array, the rest of the code will work wherever you want on the page
//# getting the main category of the page
$catid=$category[0]->category_parent;
if($catid==0){
$catid=$category[0]->cat_ID;
}
//# now letz get the children categories of the main category
$categories = get_categories('child_of='.intval($catid));
foreach ($categories as $category) {
//# check if it is a real parent category with subcategories
if ($category->parent ==$catid):
echo '<span class="category"><a href="">'.$category->cat_name.'</a></span>';
//# here we go, getting the subcategories
$subcategories= get_categories('child_of='.intval($category->cat_ID));
foreach ($subcategories as $subcategory) {
echo '<span class="subcategory" style="padding-left:12px">';
echo '<a href="">'.$subcategory->cat_name.'</a></span>';
}
endif;
}
|
* Loại bỏ wrap bao ngoài toàn bộ danh mục (ul).
wp_list_categories(array('title_li'=>''));
|
Kết quả:
<li class="cat-item cat-item-1"><a href="#" ><span>Item 1</span></a></li>
<li class="cat-item cat-item-4"><a href="#" ><span>Item 2</span></a></li>
<li class="cat-item cat-item-4"><a href="#" ><span>Item 3</span></a></li>
Lưu ý: nếu bạn thêm ký tự cách vào thuộc tính 'title_li' (ie: 'title_li' = ' '), bạn sẽ nhận được 2 lần thẻ ul bị lặp lại bao toàn bộ thẻ li. Ví dụ:
<ul class="nav-main">
<li class="product-cats">
<ul>
...li items...
</ul>
</li>
</ul>
* Lọc tham số wp_list_categories:
Bằng cách truyền tham số vào hàm wp_list_categories bạn sẽ giới hạn hiển thị categories theo ý muốn. Wordpress gọi filter 'widget_categories_args' để xác nhận tham số lần cuối. Thêm đoạn code sau vào file theme functions.php
function my_wp_list_categories($cat_args){
global $post;
$tax=get_object_taxonomies($post);
$cat_args['title_li'] = 'ABC';
$cat_args['exclude'] = 1;
$cat_args['use_desc_for_title'] = 0;
$cat_args['walker']=new Post_Category_Walker( $post->ID, $tax[1] );
return $cat_args;
}
add_filter('widget_categories_args', 'my_wp_list_categories', 10, 2);
|
Ví dụ khác hiển thị sub categories của category mẹ.
//start modify hook
add_filter('widget_categories_args', 'my_wp_list_categories', 10, 2);
function my_wp_list_categories($cat_args){
$cat_args['depth']=1;
$cat_args['child_of']=get_query_var('cat');
$cat_args['hide_empty']=0;
return $cat_args;
}
|
Ngoài ra, cách đơn giản hơn bạn thiết lập tham số trực tiếp vào hàm lấy categories 'get_categories'. VD:
get_categories('child_of='.get_query_var('cat').'&hide_empty=1');
|
* Tùy biến nội dung hiển thị của hàm wp_list_categories thông qua hook 'wp_list_categories'. Xem ví dụ sau:
add_filter('wp_list_categories','filter_wp_list_categories');
function filter_wp_list_categories($output){
....
return $output;
}
|
Bài tập: thêm class vào thẻ ul cho liệt kê danh mục.
Chúng ta sẽ sử dụng 2 hook 'widget_categories_args' & 'wp_list_categories'. Ý tưởng là xóa ul mặc định và thêm ul mới với các thuộc tính đưa vào:
//set title_li to empty string.
add_filter('widget_categories_args', 'my_wp_list_categories', 10, 2);
function my_wp_list_categories($args){
$cat_args['title_li'] = '';
}
//wrap ul tag to categories content
add_filter('wp_list_categories','filter_wp_list_categories');
function filter_wp_list_categories($output){
$out='<ul class="my-class">';
$out.=$output;
$out.='</ul>';
return $out;
}
|
* Sửa tên hiển thị category:
Trước khi hiển thị nhãn category, wordpress gọi filter 'list_cats' nhờ đó mà chúng ta có thể sửa lại mọi tên hiển thị category nếu muốn.
add_filter('list_cats','list_cats_',10,2);
function list_cats_($cat_name,$category){
return $cat_name;
}
|