Cách nhận giá trị phân loại bài đăng tùy chỉnh với các ví dụ

Để lấy giá trị của phân loại loại bài đăng tùy chỉnh, bạn có thể sử dụng chức năng `get_the_terms()` trong WordPress. Hàm này chấp nhận ID của bài đăng và tên của phân loại làm đối số và nó trả về một mảng các đối tượng thuật ngữ cho bài đăng và phân loại đó

Đây là một ví dụ

// Get the post ID
$post_id = get_the_ID();

// Get the name of the taxonomy $taxonomy_name = 'genre';

// Get the terms for the post and taxonomy $terms = get_the_terms( $post_id, $taxonomy_name );

// Loop through the terms and print their names foreach ( $terms as $term ) { echo $term->name; }

Mã này sẽ in tên của tất cả các thuật ngữ trong phân loại `thể loại` cho bài đăng hiện tại

Bạn cũng có thể sử dụng hàm `get_term_meta()` để lấy siêu dữ liệu cụ thể cho một thuật ngữ. Hàm này chấp nhận ID thuật ngữ và khóa meta làm đối số và trả về giá trị của siêu dữ liệu cho thuật ngữ đó

Đây là một ví dụ

// Get the post ID
$post_id = get_the_ID();

// Get the name of the taxonomy $taxonomy_name = 'genre';

// Get the terms for the post and taxonomy $terms = get_the_terms( $post_id, $taxonomy_name );

// Loop through the terms and print their names and descriptions foreach ( $terms as $term ) { echo $term->name;

// Get the term description $description = get_term_meta( $term->term_id, 'description', true );

// Print the term description echo $description; }

Mã này sẽ in tên và mô tả của tất cả các thuật ngữ trong phân loại `thể loại` cho bài đăng hiện tại

Tôi hi vọng cái này giúp được. Hãy cho tôi biết nếu bạn có bất kì câu hỏi nào khác

Chúng tôi cung cấp cho bạn một số câu trả lời cho truy vấn Cách nhận giá trị phân loại loại bài đăng tùy chỉnh bằng các ví dụ trong bài viết tiếp theo. Tôi hy vọng đây sẽ là câu trả lời bạn cần, giúp bạn tiết kiệm thời gian. thực thi

Cách lấy giá trị phân loại loại bài đăng tùy chỉnh bằng các ví dụ - lấy giá trị phân loại loại bài đăng tùy chỉnh
<?php $terms = wp_get_post_terms( $query->post->ID, array( 'country', 'subject' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//hook into the init action and call create_book_taxonomies when it fires
 
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
 
//create a custom taxonomy name it subjects for your posts
 
function create_subjects_hierarchical_taxonomy() {
 
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
 
  $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('subjects',array('books'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'subject' ),
  ));
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//hook into the init action and call create_book_taxonomies when it fires
 
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
 
//create a custom taxonomy name it subjects for your posts
 
function create_subjects_hierarchical_taxonomy() {
 
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
 
  $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('subjects',array('books'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'subject' ),
  ));
 
}

Còn nhiều ví dụ khác xoay quanh chủ đề How to get custom post type taxonomy value with Examples vui lòng xem thêm

Làm cách nào để tôi có được bài đăng phân loại tùy chỉnh?

Trong WordPress, bạn có thể tạo (hoặc “đăng ký”) một phân loại mới bằng cách sử dụng hàm register_taxonomy() . Mỗi tùy chọn phân loại được ghi lại chi tiết trong WordPress Codex. Sau khi thêm phần này vào các chức năng của chủ đề của bạn. php, bạn sẽ thấy một phân loại mới trong menu “Bài đăng” trong thanh bên của quản trị viên.

Làm thế nào để bạn tìm thấy giá trị phân loại?

bạn có thể lấy giá trị trường từ phân loại bằng cách sử dụng get_field() bằng cách chỉ định phân loại và ID thuật ngữ trong tham số thứ hai – nối với nhau bằng dấu gạch dưới.

Làm thế nào để bạn tìm thấy phân loại của một bài viết?

Làm cách nào để tìm phân loại từ ID bài đăng? .
// TRUY LẠI TERM SLUG ( cho đĩa đơn. php hoặc phần mẫu)
$terms = get_the_terms( $post->ID, 'your-taxonomy' );
nếu (. trống( $terms ) ){
// lấy số hạng đầu tiên
$term = array_shift( $terms );
tiếng vang $term->slug;

Làm cách nào để tìm giá trị phân loại trong WordPress?

get_taxonomies( array $args = array(), string $output = 'names', string $operator = 'and' ). sợi dây[]. WP_Taxonomy[] Truy xuất danh sách tên hoặc đối tượng phân loại đã đăng ký.