Gần đây khi thiết kế website wordpress cho một số khách hàng , mình gặp phải 1 số yêu cầu khá phức tạp mà họ đưa ra là, Trong phần bài viết ngoài chọn theo chuyên mục, còn phải hiển thị các bài viết liên quan từ taxonomy tạo ra. Cụ thể là mình làm một website âm nhạc khách hàng yêu cầu hiện thị các beat liên quàn từ cá version khác nhau và , các album khác nhau. Ngoài ra khách hàng còn yêu cầu trong phần quản lý bài viết có thể tìm kiếm các bài viết theo albums và verions.

Vậy làm thế nào để xử lý vấn đề này ?

Để giải quyết yêu cầu này, đơn giản là mình tạo ra 2 taxonomy có chức năng tạo ra 2 loại albums và verions trong wordpress Để làm việc này bạn chỉ cần sử dụng 2 functions sau trong file functions.php

[crayon] // add list mau web
add_action( ‘init’, ‘create_list_products_taxonomies’,1);
function create_list_products_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
‘name’ => __( ‘Danh mục Albums’),
‘singular_name’ => __( ‘Danh mục Albums’),
‘search_items’ => __( ‘Tìm kiếm Danh mục Albums’ ),
‘all_items’ => __( ‘Tất cả Danh mục Albums’ ),
‘parent_item’ => __( ‘Danh mục cha’ ),
‘parent_item_colon’ => __( ‘Danh mục cha:’ ),
‘edit_item’ => __( ‘Chỉnh sửa Danh mục Albums’ ),
‘update_item’ => __( ‘cập nhật Danh mục Albums’ ),
‘add_new_item’ => __( ‘Thêm mới Danh mục Albums’ ),
‘new_item_name’ => __( ‘Thêm mới Danh mục Albums’ ),
‘menu_name’ => __( ‘Danh mục VAlbums’ )
);
register_taxonomy(‘list-products’, array(‘post’), array(
‘hierarchical’ => true,
‘labels’ => $labels,
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘danh-muc’,’with_front’=>false),
));
}

// add list version
add_action( ‘init’, ‘create_list_version_taxonomies’,2);
function create_list_version_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
‘name’ => __( ‘Danh mục Version’),
‘singular_name’ => __( ‘Danh mục Version’),
‘search_items’ => __( ‘Tìm kiếm Danh mục Version’ ),
‘all_items’ => __( ‘Tất cả Danh mục Version’ ),
‘parent_item’ => __( ‘Danh mục cha’ ),
‘parent_item_colon’ => __( ‘Danh mục cha:’ ),
‘edit_item’ => __( ‘Chỉnh sửa Danh mục Version’ ),
‘update_item’ => __( ‘cập nhật Danh mục Version’ ),
‘add_new_item’ => __( ‘Thêm mới Danh mục Version’ ),
‘new_item_name’ => __( ‘Thêm mới Danh mục Version’ ),
‘menu_name’ => __( ‘Danh mục Version’ )
);
register_taxonomy(‘list-version’, array(‘post’), array(
‘hierarchical’ => true,
‘labels’ => $labels,
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘list-versions’,’with_front’=>false),
));
}
[/crayon]

Mặc định trong wordpress phần quản lý post chỉ hỗ trợ lọc theo tháng, chuyên mục, Để lọc theo các taxonomy tạo ra thì bạn tự thêm code vào. Sau đây dịch vụ thiết kế web du lịch giá rẻ bằng WordPress sẽ hướng dẫn thêm phần lọc đó chỉ bằng hàm sau trong file funtions.php

[crayon]

function rudr_posts_taxonomy_filter() {
global $typenow; // this variable stores the current custom post type
if( $typenow == ‘post’ ){ // choose one or more post types to apply taxonomy filter for them if( in_array( $typenow array(‘post’,’games’) )
$taxonomy_names = array(‘list-version’, ‘list-products’);
foreach ($taxonomy_names as $single_taxonomy) {
$current_taxonomy = isset( $_GET[$single_taxonomy] ) ? $_GET[$single_taxonomy] : ”;
$taxonomy_object = get_taxonomy( $single_taxonomy );
$taxonomy_name = strtolower( $taxonomy_object->labels->name );
$taxonomy_terms = get_terms( $single_taxonomy );
if(count($taxonomy_terms) > 0) {
echo “




“;
}
}
}
}

add_action( ‘restrict_manage_posts’, ‘rudr_posts_taxonomy_filter’ );

[/crayon]

Sau khi bạn thêm phần này vào, khi truy cập vào quản lý bài viết sẽ thấy thêm phần lọc theo albums và verion.

Lọc bài viết theo taxonomy trong wordpress

5/5 - (2 bình chọn)

Trả lời

Chat Zalo

0932644183

Chat Zalo

0932644183