Wordpress uses the get_the_category function to obtain the classification data of the current article.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.