Get_the_category () & nbsp; return the array set of categories to which the current article belongs. usage: & lt ;? Phpget_the_category ($ id )? & Gt; parameter: $ id & nbsp; article id. the default value is the ID of the current article. the type is an integer. optional values: array example:
Get_the_category ()??? Returns the array set of the category of the current article.
Usage:
Parameter: $ id?? Article id. the default value is the current article ID. the type is an integer. optional.
Returned value: Array
Example:
Eg1: If you want to use a value in the returned category directory, you must specify an array index. As you can understand, an article may belong to multiple classification directories and traverse the returned results to obtain the objects of each category, you can use a member to obtain the desired category ID and category name and other content.
???$ Category = get_the_category ();??? // Obtain the current Category ID by default
???Echo $ category [0]-> cat_name ;???? // You cannot obtain the correct value using $ categories-> cat_name. you must use $ categories [0]-> cat_name to operate correctly.
?>
Eg2: loop through the document page. the output image is named by Category ID, and the alt attribute is set to the category name.
???? Foreach (get_the_category () as $ category ){
????????? Echo 'cat_ID.'.jpg "alt =" '. $ category-> cat_name.' "/> ';
}
?>
Eg3: displays the titles of all articles in the current category based on the current Category id.
???$ Cat = get_the_category ();
???$ Cat_id = $ cat [0]-> cat_ID;
???Query_posts ('Order = asc & cat = '. $ cat_id );
???While (have_posts (): the_post ();
?>
">
? Endwhile; wp_reset_query ();?>
Eg4: on the document page, you can use the specified document ID to obtain information about the document category. Such as the category name, category alias, category description, and number of articles contained in the category.
???Global $ post;
???$ Categories = get_the_category ($ post-> ID );
???// Var_dump ($ categories );
???Echo $ categories [0]-> cat_name;
???Echo $ categories [0]-> category_nicename;
???Echo $ categories [0]-> category_description;
???Echo $ categories [0]-> category_count;
?>
Meanings of returned values:
Cat_ID
??? Category id
Cat_name
??? Category name
Category_nicename
??? An slug, that is, the url-friendly name generated by the system based on the category name
Category_description
??? Category description
Category_parent
??? Parent Category id
Category_count
??? How many logs are contained in the category or how many times the category is used