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
- Display All Products by Category with WooCommerce
- Woocommerce get category image full size
- Control term order on a per-post basis
- Rewrite URL with category and tag combined using WP_Rewrite
- How to tax query X number of posts related by tag first, then by category if not enough in tag-related
- Create subdomains for tags and categories
- Automated adding of one tag to all the posts in a category
- Moving Categories submenu to Media, but still opens Posts menu
- How do I edit pre_get_post for the category the user is currently in?
- When using get_categories or similar, is it possible to filter results that contain certain Tags as well?
- Display WooCommerce product categories in a 4 columns custom menu [closed]
- How to select product category while adding new product in woocommerce? [closed]
- Deleting All tags except categories Wp database
- Is it possible to put tags and category information in the post as text?
- Search by word, category, tag, author
- How do I count how many top level categories there are?
- How to filter archives both by category and tag?
- display most popular tags of category?
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Display “add to cart” button on every listing in product category page?
- Custom WP_Query for current category (in category.php)?
- Getting categories id for all products in cart
- Are Category or Tag Archive Pages Possible?
- Woocommerce category show subcategories only/omit posts for specific category
- products nested by subcategories in a current category archive
- How can I duplicate categories and tags?
- WC 3.x Get categories from variable product of the cart
- Random post + categories + tags
- RSS/XML of all Categories and/or Tags
- Get Tags specific to Category using WooCommerce plugin
- Hide woocommerce category name from specific pages
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- Pagination not visible on woocommerce’s category page
- Remove product category placeholder image (Woocomerce)
- Needing a snippet for listing category specific tags
- Change thumbnail image depending product category
- Unwanted Duplicate Product Categories in WooCommerce
- Woocommerce custom category pages
- Customize WordPress widget – Product Category
- How to list categories within tags?
- How to “group” (categorize) the Pages together?
- Is There a Difference Between Taxonomies and Categories?
- Use Tag input metabox style for Categories
- Custom SQL Query: Get all posts with category id and a concated list of tags on each post
- Have multiple category queries from the URL been fixed yet?
- Related Tags not in category
- Categories are not listing their respective products
- Fix Warnings that shouldn’t be necessary to fix
- Include WooCommerce product to all child categories
- Single.php Category Post Count
- scaleability of tags
- How do I add a tag slug to a category URL to filter posts?
- How replace individual elements in the RSS feed with a single string
- Adding a H1 Tag to Post Tags automatically, but hide the tag? (Same for Category)
- Apply WordPress function on specific woocommerce product categories only
- Adding custom ID field to the categories
- How to display product with two condition, category and brand taxonomy
- Conditional category & tag statement
- Include either name or slug in the include parameter using wp_list_categories?
- How can I filter posts by tag on the fly
- Advanced permalinks structure
- How can I include custom category and tag base in template files?
- Custom menu with categories and tags
- category description not showing in my woocommerce
- How to make product image inherit from category image
- 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?
- How to add all subcategories as submenu in WordPress menu
- echo $category[0]-> cat_name; shows only one category name
- in post content shortcode works, but hardcoded in same page template doesn’t?
- 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 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
- Add subcategories posts to the counts column at the admin’s categories list
- Category and tag urls return 404 error
- How to echo Tags and Categories as plain text
- How to exclude category and post_tag taxonomy while displaying custom taxonomy?
- 404 error when i try to search by category or by tag
- How to remove specific categories from tag archives?
- A simple question regarding tags and categories
- Display related posts with same category or same tag return blank
- How can I allow people to ‘subscribe’ to categories?
- Loops in category description
- How to create a category or tag available exclusively to the admins and not to the users?
- List products from current category
- Custom Woocommerce Category view
- Displaying categories and subcategories without link
- Sorting Woocommerce products by category and attributes
- How to get Woocomerce categories count
- Categories and products woocommerce wordpress
- Restricted category in Woocommerce [closed]