Use wp_get_post_categories()
Retrieve the list of categories for a post.
<?php wp_get_post_categories( $post_id, $args ) ?>
Be aware that the function returns an array (of category ids) even if you only have one category in your post.
The example below shows how categories are retrieved, and then additional information is retrieved for each category.
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
Reference: http://codex.wordpress.org/Function_Reference/wp_get_post_categories
Another Option:
Use get_the_terms();
<?php
$id = get_the_id();
$terms = get_the_terms( $id, 'category' );
// print_r( $terms );
foreach($terms as $term) {
echo $term->cat_ID;
}
?>
Related Posts:
- How do I get the category URL from get_the_category?
- Create single.php for specific category by category id
- in_category() works in single.php but not in page.php?
- previous/next_post_link in the same sub-category?
- Send to different single pages by category when multiple categories exist
- How to make the link to the category for a post go to corresponding page number in archive?
- Single.php Category Post Count
- Problems with my conditionals in single.php by category
- Get next post in same category not working WP 4.7.4
- Simple way to style posts of a single category differently in the loop and in single
- How to get nav to show current_page_parent class when on regular post (not blog), differentiated by category
- How can I check which archive has referred the single? [closed]
- Displaying links to all posts of the same category on the post page
- Ignore the Ad on Specific Categories
- custom wp_list_categories in single.php
- loop in single.php of the same category
- Display Some text in some specific category not all category
- When category name and title are the same … WP will select archive.php instead of single.php
- Using template file for single post in sub-directory multisite
- Code to display category-specific single.php is overriding normal single.php, too
- Display Referring Category on single post?
- Modify / Rewrite single template url structure
- How do I get a single page navigation depending on the previous page?
- Is There a Difference Between Taxonomies and Categories?
- WooCommerce: List All Categories
- Get rid of WordPress category, tag and author archives?
- How can I hide a category from Contributors in the edit/add new post screen?
- Filtering categories in the permalink structure
- Wrap Post count in a span
- wp_nav_menu not appearing for a couple pages
- Why aren’t sub-categories nested after creating post in the WP Admin?
- Preventing index.php?category_name=something from redirecting
- How to get category id of current post?
- Trash bin for categories
- Display one category in one page
- How to get next previous category in same taxonomy?
- Show children of top level category only
- is_archive() content being shown instead of is_category()
- how do I filter single_cat_title to remove all instances of a particlular word
- If a post has two categories with different permissions, what will happen? [closed]
- Dropdown category filter
- Category page shows Oops! That page can’t be found
- How to find the number of Tags a post has?
- How can I enable a TINYMCE rich text editor in the admin interface?
- Add title, post content, and category like add_post_meta and update_post_meta
- How to create button to direct to certain category
- Highlight wp_nav_menu when category is selected
- query order by category
- How to make 2 tag feeds show up on 1 page?
- Best way of getting WooCommerce category path
- Display the last post by each category?
- Order categories by name or view count
- How to assign posts without any category to the default category
- Give a page priority over a category that has the same name?
- Filter $cpt categories to omit certain category
- How to rewrite the post URL according to the category?
- ajax for filtering posts by category in wordpress loops
- Create category post shortcode
- Question on using custom structures for categories
- Wrong category nice name is displayed on archive page
- Cannot filter categories by parent category
- Redirect year month day postname permalink structure to category postname structure
- Show all sub categories?
- Category nicename inside loop in wordpress
- Generating a feed of all but one category of posts
- Displaying another main nav menu for specific category
- Exclude categories from list (wp_list_categories)
- How to display the 5 latest post titles but allow only 1 in there of a specific category
- How to add categories using mysql
- Posts made to category don’t show up (do show up on frontpage)
- Display all categories but only if they have posts in them that have a specific tag assigned
- How to fix select filter on category
- wp_dropdown_categories() works correctly but the list is not filtered in admin for custom post type. What is the problem?
- Add $args to wp_list_categories
- Show audio player only in specific post type category
- Check with Jquery if second level checkbox from categories are checked
- Regarding Tags And Categories
- How to show entries related to main category
- selecting categories that have products with tag/category
- Restrict retrieved terms by category?
- How to get WooCommerce Product Categories and its children’s children?
- Get category from slug list
- Media Library Category Exclude Tree
- Re-coding category-template.php to go to custom URL
- Category removal – Portfolio Category:
- How get all media from a posts category by db?
- Cant get unique_array() work on get_the_category() foreach loop
- Show One Level Category id
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Custom filters inside a specific category?
- how to Specifies an author / editor to edit one category only?
- Function to list posts from current post’s category fails in WP 3.8
- Formating content in category.php
- How to use Greek characters/letters in a query?
- Getting categories to change appropriately when using $cat = ‘ ‘;
- Display post content from category name using ajax
- why is the delete option missing from just one category?
- Echo Category In Loop
- Get all categories
- Show category post order ASC