Get categories for a specific post

     
Is it possible khổng lồ get the category name of a category given the Post ID, the following code works to get the Category Id, but how can I get the name?

Thank!




Bạn đang xem: Get categories for a specific post

*

*

Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
here you go get_the_category( $post->ID ); will return the array of categories of that post you need lớn loop through the array

$category_detail=get_the_category("https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg4"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg);//$post->IDforeach($category_detail as $cd)echo $cd->cat_name;get_the_category


*

*

*

function wp_get_post_categories( $post_id = 0, $args = array() ) $post_id = (int) $post_id; $defaults = array("https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgfields"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg => "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgids"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg); $args = wp_parse_args( $args, $defaults ); $cats = wp_get_object_terms($post_id, "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgcategory"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg, $args); return $cats;Here is the second argument of function wp_get_post_categories() which you can pass the attributes of receiving data.

$category_detail = get_the_category( "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg4"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg,array( "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgfields"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg => "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgnames"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg ) ); //$post->IDforeach( $category_detail as $cd ) echo $cd->name;


Xem thêm: Làm Hình Ảnh Có Tên Mình Trực Tuyến, Tạo Hiệu Ứng Chữ Mầm Ánh Sáng

You can a single line to lớn echo out the category name by passing the post ID simply by using this:

echo wp_get_post_terms(get_the_ID(), "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgcategory"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg)<0>->name;


Use get_the_category() function.

$post_categories = wp_get_post_categories( 4 );$categories = get_the_category($post_categories<0>);var_dump($categories);
term_id; $cat->name; ?> term_id, "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgthumbnail_id"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg, true ); $image = wp_get_attachment_url( $thumbnail_id ); if ( $image ) echo "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg

*
"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg; }?>
First Category name by post id

$first_category = wp_get_post_terms( get_the_ID(), "https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpgcategory"https://jdomain.vn/wp-get-post-categories/imager_6_10859_700.jpg )<0>->name;
Thanks for contributing an answer to Stack Overflow!

Please be sure khổng lồ answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy & cookie policy


mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... Expects parameter 1 to lớn be resource
Site kiến thiết / logo sản phẩm © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2023.1.16.43160


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device và disclose information in accordance with our Cookie Policy.


Chuyên mục: Domain Hosting