Hiển thị tất cả category của custom post type | get the terms

https://levantoan.com/hien-thi-tat-ca-category-cua-custom-post-type-get-the-terms/

Mô tả chức năng

Khi ta có 1 custom post typeProduct thì không thể dùng the_category() để hiển thị các category có trong bài được mà phải dùng hàm khác. Đó chính là get_the_terms.

Cách sử dụng

//Hàm get_the_terms để hiển thị các category của bài viết dùng custom post type
<?php 
 get_the_terms( $id, $taxonomy ); 
?>

Thông số

$id: Là ID của bài viết, bắt buộc và mặc định là 0. Khi trong vòng while() thì chúng ta thường dùng biến $post->ID để lấy được ID của bài viết

$taxonomy: là chuỗi, hoặc nằm trong array. Giá trị này bắt buộc phải có. Đây là tên taxonomy của custom post type. VD: category, sanpham, product_svl …. (Cách tìm: Tại đường dẫn admin như sau thì ta có thể biết dc tên taxonomy wp-admin/edit-tags.php?taxonomy=category tại ví dụ này thì $taxonomy = category)

Giá trị mà get_the_terms trả về

Khi chúng ta gọi hàm get_the_terms thì hàm này sẽ ra về kiểu mảng (array), false hoặc wp_error. Dưới đây là thống kê các tham số có trong mảng

1234567891011121314

stdClass Object( [term_id] => [name] => [slug] => [term_group] => [term_order] => [term_taxonomy_id] => [taxonomy] => [description] => [parent] => [count] => [object_id] =>)

Ví dụ minh họa

Ví dụ này sẽ hiển thị category của 1 bài viết có tên taxonomy product_svl

<?php
$terms = get_the_terms( $post->ID, 'product_svl' ); 
if ( $terms && ! is_wp_error( $terms ) ) :     
$draught_links = array();     
foreach ( $terms as $term ) {        
    $draught_links[] = $term->name;    
}     
$on_draught = join( ", ", $draught_links );?> 
<p>    Chuyên mục: <span><?php echo $on_draught; ?></span></p> 
<?php endif; ?>

Kết quả trả về như sau: Chuyên mục: cat1, cat2, cat 3 …

Cách dùng: $term->name để thấy tên,$term->count để lấy được số bài viết có trong cat và nhiều giá trị khác các bạn xem thêm ở phần giá trị trả về nhé

Mở Rộng

Code dưới đây có thể giúp bạn hiển thị toàn bộ category của bài viết dùng custom post type và bao gồm cả link

Chèn đoạn code này vào file functions.php

12345678910111213141516171819202122232425262728293031323334

<?php// get taxonomies terms linksfunction custom_taxonomies_terms_links(){ // get post by post id $post = get_post( $post->ID ); // get post type by post $post_type = $post->post_type; // get post type taxonomies $taxonomies = get_object_taxonomies( $post_type, 'objects' ); $out = array(); foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){ // get the terms related to post $terms = get_the_terms( $post->ID, $taxonomy_slug ); if ( !empty( $terms ) ) { $out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>"; foreach ( $terms as $term ) { $out[] = ' <li><a href="' . get_term_link( $term->slug, $taxonomy_slug ) .'">' . $term->name . "</a></li>\n"; } $out[] = "</ul>\n"; } } return implode('', $out );}?>

Sau đó chèn đoạn code sau vào chỗ bạn muốn hiển thị trong vòng lặp while() nhé

1

<?php echo custom_taxonomies_terms_links(); ?>

Chúc các bạn thành công ^^

Xem thêm: http://codex.wordpress.org/Function_Reference/get_the_terms

Last updated