wp_get_post_categories()
and get_category()
are specifically for the category
taxonomy. You’re using a custom taxonomy, portfolio_cat
, so you need to use get_the_terms()
to get categories.
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str="";
foreach ( $post_categories as $category ) {
$cat_str .= $category->name;
}
If you just want a comma separated list of category links, you can use get_the_term_list()
to simplify the code:
$cat_str = get_the_term_list( $post_id, 'portfolio_cat', '', ', ', '' );
Or, if you don’t want links, you can do this instead:
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str = implode( ', ', wp_list_pluck( $categories, 'name' ) );
Also, instead of $query->post->post_title
you should really just be using get_the_title();
.
Related Posts:
- How can i list custom post type categories?
- Add category attribute to custom shortcode
- Get Posts by Category, Tag , and CPT Taxonomy
- How to seperate posts by categories?
- How to call Shortcode categories for custom post types?
- Using get_terms() as shortcode attribute
- How to separate posts by categories?
- Categories sorting
- How can I make my custom posts appear in their assigned category url?
- How to call in Custom Post-Type Categories?
- Allow user to set custom order to a list of custom taxonomies?
- get_category_parents for custom post type taxonomy
- Separate custom categories from default category
- forming WP_Query for posts of all post types but from specific categories
- wp_list_categories() – current-cat class also inside posts?
- Showing posts from different categories and from custom post type
- Custom post type getting wrong categories and tags
- Categories of custom taxonomy don’t show any posts
- How to display custom taxonomies with links in filter menu?
- Show a Category X’s custom post type on Category X archive page?
- PHP variable not regenerating when publishing multiple posts at the same time
- Multiple level category drop-down from the WordPress dashboard
- Cannot add category or custom taxonomy from admin. WordPress site is hosted in Windows 16 server via IIS. No XAMPP or WAMP
- Add custom post type settings to wordress default posts
- Hide parent categories when clicked, and show it’s childs
- Limit amount of posts made within a custom taxonomy
- Problem with displaying posts in the CPT category
- Filter CPT posts by one or more categories
- Getting categories of posts under a custom taxonomy
- Custom Post Types with a common category for a blog listing
- Can’t remove front from permalinks for custom taxonomy category page
- How to get categories linked in posts for a specific post type
- Custom post type, organized by categories
- How to display “Category and Post_tag” component in a CPT Gutenberg edit screen?
- Using advanced custom fields from one custom post type in another custom post type / using nested shortcodes
- How to render a custom post type template with custom fields using shortcode
- Remove CPT name from permalink but add %category% instead
- Set a checkmark in a category based on a URL-parameter
- Form action/link to render a plugin in WordPress front-end
- WP Query with categories only shows one post and ignores the category
- custom post type category count shortcode
- Adding custom post type to count in category
- How to edit this code to get the categories in achieve page?
- Category with post type pagination returns 404
- Display Child Pages of Custom Post Type Parent Page
- Custom Post Type and Category Base Rewrite Issue
- Get the category from custom post type
- Post injections into Site Origins Page builder [closed]
- Custom Post Type + Categories
- How to create a gallery page with categories?
- Custom Post Slug same as Parents Category Slug
- Shortcode to display Staff post type based on Location post type and Specialty post type
- WordPress posts page for customer filtering and sorting along with category filter
- I am having Trouble to get list of Categories of Custom post Type
- Hierarchy and access control for Custom Post Types (CPT)
- Multiple (two) category postings on the same page.
- Loop filtering Custom Post Types and/or Categories
- School & class blogs: Categorize with categories or custom posts?
- Custom post type and category link together
- Why is my category template ignoring post type?
- Best method to make posts searchable, sortable and filterable – custom field, tag or category?
- How to get custom posts sub category link
- wp_reset_postdata() and wp_reset_query() inside shortcode are not working to reset original page query
- Shortcode for CPT post content in a page
- Not sure if I should create multiple custom post type [closed]
- Cross reference custom post types
- Make a custom_post translatable
- Pre-selecting the category for a custom post type
- Next Post links not working in custom post type shortcode
- Foreach inside shortcode not working as planned
- browse by category and tags?
- I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
- Categories filtering in new post
- Conditional statement for if archive page has posts which contain certain taxonomies/categories/tags, show those terms
- Custom post types and category archive
- List of Posts and Categories
- How can I show 1 featured post in a styled element, and the next few below differently styled
- How do you create a custom template to display a category with an image and related posts below?
- How can I list Custom Post Types created with the Types plugin under categories?
- List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
- Custom Post Type Category Link
- Custom Post type category pages template and loop
- Custom templates for a specific category
- Redirect to another page using contact form 7? [closed]
- CPT category hierarchy
- Categorise Custom Post Types
- WP the_posts() on single-cars.php get category link
- Can a shortcode included in post content add styles to the ?
- Category.php template for custom posts
- Categories and tags for custom post types
- Create new custom post and post category of same name
- Tag page only display 10 posts
- category page for custom post type
- in_category for custom post types
- Checking if looped item has a parent inside a shortcode
- Get parent category id from child category page for custom taxonomy
- Getting used tags per post type
- Archieve.php not loading for custom post type
- multiple custom post type on category page
- get_post_meta is not working inside shortcode