Let’s take a look at get_the_terms
Codex page. We can find there that this function may return:
(array|false|WP_Error) Array of WP_Term objects on success, false if
there are no terms or the post does not exist, WP_Error on failure.
So your code will work correct only in one case – when the function returns an array of terms.
But it may also return a WP_Error
if any error occurs or false
if there are no terms for that post in given taxonomy.
So yes – you should always include proper error checking in your code. It may look like this:
global $product, $post, $smof_data;
$cats = get_the_terms($post->ID, 'product_cat');
$cat_count = ( !empty( $cats ) && ! is_wp_error( $cats ) ) ? count($cats) : 0;
$tags = get_the_terms($post->ID, 'product_tag');
$tag_count = ( !empty( $tags ) && ! is_wp_error( $tags ) ) ? count($tags) : 0;
Related Posts:
- Pagination is not working properly in Product Category/Tag pages
- Show the amount of posts in a tag in a specific category has
- Display all product tags associated with a specific product category
- Count how many posts have a specified tag AND category
- Categories and Tags Conflict after Woocommerce Installation
- Change categories count to include media attachments
- Template for product-category page [closed]
- Tags Instead of Category
- How to exclude posts from category posts count
- Conditional category & tag statement
- Use blog as base for tags and categories
- Include either name or slug in the include parameter using wp_list_categories?
- List all posts of certain category which share some tags
- How to store category and tags separatly on wordpress?
- How can I filter posts by tag on the fly
- Retrieve post tags from from main wp_query
- Advanced permalinks structure
- Filter categories/tags to display in multiple metaboxes for selection in back-end
- post_class remove tag- or category- from slug
- Editing Loop So It Targets Specific Tags?
- How can I include custom category and tag base in template files?
- Pretty URLs with Categories and Tags
- Autolinking keywords in text with tags
- Maintaining a separate posts page for certain categories and don’t show those on home page
- Are tags different than categories?
- What is the best way to add an advanced / intermediate / beginner tag to a post
- Custom menu with categories and tags
- WooCommerce shared categories
- displaying the last product comment in the woocommerce category
- Displaying Single Product Page Parent and Child Category
- Show Posts via cat+cat in URL that are in both Categories doesn’t work
- Display all categories but only if they have posts in them that have a specific tag assigned
- display specific category when website loads
- Woocommerce only display child category when parent is checked (on add new product page)
- Help to display the current categories and tags of a post
- Show the number of the post
- Automatically adding tags and categories into Post Excerpt for searching
- How to add tags under categories
- category description not showing in my woocommerce
- Add Post Categories in Post Tags Field Programmatically
- Return all Tags and Categories in Separate List
- Conditional label Woocommerce archive [unsolved]
- How to filter tags by category?
- Show audio player only in specific post type category
- How to make product image inherit from category image
- List all categories and count number of posts within based on custom date
- How to show given products only using product id array from specific category page in woocommerce
- Shop Category Pages missing s
- How to set acf color field as background color to product category
- How to target all woocommerce categories that don’t have any subcategories in them?
- WooCommerce REST API not returning the default products category
- In if…else condition, the else statement shows even if if statement is correct
- How to show WooCommerce Categories on ‘shop’ page instead of products?
- Show the parent taxonomy by creating shortcode in woocommerce?
- How can I display all products from all categories on one page with woocommerce?
- How to add all subcategories as submenu in WordPress menu
- How to edit woocommerce sub-category page
- How to prevent tag “iframe” from being deleted from the heading description of rubric?
- When creating a new product, auto assign it to all custom taxonomy woocommerce
- Hide specific category tag on single post page
- Filtering posts in category by tag URL
- echo $category[0]-> cat_name; shows only one category name
- How to show entries related to main category
- Get WooCommerce product category list in functions.php
- in post content shortcode works, but hardcoded in same page template doesn’t?
- Automatically create tags and merge with categories
- Is there a reason to put a product in both the Child and Parent Categories?
- WordPress category title not update in navigation menu
- WooCommerce showing active product categories on single product page
- selecting categories that have products with tag/category
- Restrict retrieved terms by category?
- When creating category, it creates automatically tag, or, when creating tag, it creates automatically category in same name
- How to fix 404 error in pagination in categories of woocommerce with common url base /shop/?
- How to display custom fields as table in Woocommerce
- How do I merge WooCommerce categories with the same name?
- Woocommerce Category Sort Dropdown
- Woocommerce: all products of all categories instead products of one category
- How to delete WooCommerce categories in bulk that only have one product
- Product Category page showing all products
- How to load terms of a custom taxonomy of a product in woocommerce cart page
- Using heirarchial tags or Custom taxonomies
- How to assign Categories and tags inside the TinyMCE Editor for each Image?
- product_cat image url from database
- How to get WooCommerce Product Categories and its children’s children?
- Woocommerce change tag_id to name
- Woocommerce product catalog, products with different description
- WooCommerce – Get products from category right before deleting category
- Add subcategories posts to the counts column at the admin’s categories list
- Re-coding category-template.php to go to custom URL
- Balance Tags to the_content Words Length
- set a parent category in a product woocommerce
- Category and tag urls return 404 error
- No results found for second level category
- How to display products with multiple conditions, product_id and category_id
- custom browse by
- Search by tag, category and author without plugin
- Limit Category/Tag Dropdown To blog_id in Multisite Install With Global Categories/Tags
- How to echo Tags and Categories as plain text
- How can I add metaboxs to Admin Bar?
- Split posts into 2 separate streams