描述
通过将post ID作为参数传递,可以在循环外部使用此函数。
注意:此函数仅返回默认“category”分类法的结果。对于自定义分类法,请使用get_the_terms()。
参数
- $post_id
- 
(int) (可选) 文章 ID,默认为当前文章 ID。 默认值: false 
返回
(WP_Term[]) WP_Term对象数组,文章的所有分配类别。
源码
更新日志
| 版本 | 描述 | 
|---|---|
| 0.71 | 开始引入 | 
使用示例
- 仅显示第一个类别名称 - $categories = get_the_category(); if ( ! empty( $categories ) ) { echo esc_html( $categories[0]->name ); }- (回显 - $categories的第一个数组元素([0])。)- 将第一个类别链接到类别页面: - $categories = get_the_category(); if ( ! empty( $categories ) ) { echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>'; }
- 函数响应示例: - $categories = get_the_category(); var_dump($categories); - array(1) { [0]=> object(stdClass)#310 (17) { ["term_id"]=> &int(6) ["name"]=> &string(10) "familylife" ["slug"]=> &string(10) "familylife" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(6) ["taxonomy"]=> string(8) "category" ["description"]=> &string(0) "" ["parent"]=> &int(0) ["count"]=> &int(208) ["object_id"]=> int(7729) ["filter"]=> string(3) "raw" ["cat_ID"]=> &int(6) ["category_count"]=> &int(208) ["category_description"]=> &string(0) "" ["cat_name"]=> &string(10) "familylife" ["category_nicename"]=> &string(10) "familylife" ["category_parent"]=> &int(0) } }
- 如果您有自定义的post_type,请获取post类别 - <?php /* FIRST * Note: This function only returns results from the default “category” taxonomy. For custom taxonomies use get_the_terms(). */ $categories = get_the_terms( $post->ID, 'taxonomy' ); // now you can view your category in array: // using var_dump( $categories ); // or you can take all with foreach: foreach( $categories as $category ) { echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />'; }
- 将所有类别显示为链接这会将分配给文章的所有类别输出为链接。必须在循环内使用。您也可以为此使用函数get_the_category_list()。 - $categories = get_the_category(); $separator = ' '; $output = ''; if ( ! empty( $categories ) ) { foreach( $categories as $category ) { $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator; } echo trim( $output, $separator ); }
- 从循环外获取文章类别 - <?php $post = get_post(); if ( $post ) { $categories = get_the_category( $post->ID ); var_dump( $categories ); }
- 显示包含名称和说明的所有类别 - <div> <?php foreach((get_the_category()) as $category){ echo $category->name."<br>"; echo category_description($category); } ?> </div>
- 要显示与文章相关的类别列表(用逗号分隔),请编写以下代码: - $cats = array();
 foreach (get_the_category($post_id) as $c) {
 $cat = get_category($c);
 array_push($cats, $cat->name);
 }- if (sizeOf($cats) > 0) {
 $post_categories = implode(', ', $cats);
 } else {
 $post_categories = 'Not Assigned';
 }- echo $post_categories;
- 用逗号显示类别列表 - <?php $categories = get_the_category(); $category_list = join( ', ', wp_list_pluck( $categories, 'name' ) ); echo wp_kses_post( $category_list ); 
- 显示类别图像这将输出以 - cat_ID命名的类别图像,alt属性设置为- cat_name。也可以使用任何其他成员变量。- <?php $categories = get_the_category(); foreach ( $categories as $category ) { echo '<img src="' . esc_url( 'http://example.com/images/' . intval( $category->term_id ) . '.jpg' ) . '" alt="' . esc_attr( $category->name ) . '" />'; }
