To properly display this page you need a browser with JavaScript support.
Hiển thị ảnh đại diện cho category/taxonomy .
Có 2 cách bạn có thể thêm ảnh đại diện cho danh mục, thêm một trường nhập ảnh vào từng trang sửa danh mục như thế này.
Cách 1 : thêm trường ảnh cho taxonomy với plugin 'categories-images'.
Tải và cài đặt plugin: https://wordpress.org/plugins/categories-images/
Sau khi kích hoạt bạn thiết lập cấu hình 'Settings->Categories Images' để kích hoạt hoặc không loại bỏ tính năng tạo ảnh ra khỏi taxonomy.
Sử dụng :
- Lấy ảnh đại diện cho danh mục trong template category.php và taxonomy.php, gọi hàm sau:
<?php
if (function_exists('z_taxonomy_image_url')) {
echo z_taxonomy_image_url();
}
?>
- Lấy bất kỳ ảnh của category nào thông qua ID.
<ul>
<?php foreach (get_categories() as $cat) : ?>
<li>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a>
</li>
<?php endforeach; ?>
</ul>
- Đối với taxonomy:
<ul>
<?php foreach (get_terms('your_taxonomy') as $cat) : ?>
<li>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<a href="<?php echo get_term_link($cat->slug, 'your_taxonomy'); ?>"><?php echo $cat->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
Cách 2 : Sử dụng plugin tạo custom fields (ACF).
Chọn Taxonomy Term với tên taxonomy bạn muốn cài đặt ảnh.
Nhấn Publish và ngay lúc này bạn mở trang sửa category sẽ thấy hiển thị thêm trường chọn ảnh. Để truy xuất ảnh trên website bạn sử dụng hàm get_field() .
Ví dụ tên trường ảnh bạn tạo nhóm cho taxonomy là 'image'.
$term = $wp_query->queried_object;
//Retrieve a field
$t = get_field('image', $term);
Hiển thị trường ảnh trong danh sách hiển thị category/taxonomy .
Tùy biến hiển thị trang liệt kê category bằng cách hiển thị thêm giá trị trường vào cột mới. Tạo hàm với hook thêm cột mới và hook còn lại để hiển thị giá trị của cột như sau:
//hiển thị columns taxonomy
add_filter("manage_edit-category_columns", 'theme_columns');
function theme_columns($columns) {
if ( !isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'category' )
return $columns;
//prepare new columns
$new_columns = array(
'cb' => '<input type="checkbox" />',
'name' => __('Name'),
'image' => 'Image',
'description' => __('Description'),
'slug' => __('Slug'),
'posts' => __('Posts')
);
//Or add 'My Column'
$columns['my_column'] = 'My Column';
return $new_columns;
}
//nội dung cột taxonomy
add_filter("manage_category_custom_column", 'manage_theme_columns', 10, 3);
function manage_theme_columns($out, $column_name, $term_id) {
//$term = get_term($term_id, 'category');
switch ($column_name) {
case 'image':
// get header image url
//$data = maybe_unserialize($theme->image);
$img=get_field("image","category_$term_id");
if(is_array($img)) $img=$img['url'];
if(!$img) $img=get_bloginfo('stylesheet_directory').'/images/placeholder.jpg';
$out .= "<img src=\"$img\" width=\"100\" height=\"83\"/>";
break;
default:
break;
}
return $out;
}
Unregistered version