" Invalid argument supplied for foreach()" .
means that the $parents
variable is not an array so you can’t loop through it. You should wrap the foreach
loop in an if
check that verifies that $parents
is something valid.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parents = get_the_terms($term->parent, get_query_var('taxonomy') );
if( $parents && ! is_wp_error($parents) ) :
echo "<div class="product-cat">";
echo "In"." "." ";
foreach( $parents as $parent ) {
echo '<a href="'.get_term_link($parent).'">'.$parent->name.'</a>';
}
echo "</div>";
endif;
?>
Though secondarily…. get_query_var('term')
and get_query_var('taxonomy')
are not returning anything on the search page because those query vars don’t pertain to the search query.
Related Posts:
- WooCommerce: List All Categories
- Combine two taxonomies in a hierarchical tree
- Woocommerce product categories order [closed]
- How to split the categories of subcategories with thumbnails
- WooCommerce – Display product child categories from specific parent
- How to order categories in Woocommerce that are spread over multiple pages? [closed]
- How to add categories to products in woocommerce programatically? [closed]
- Display products for a category, sorted by post_excerpt
- Woocommerce – Get product category 2nd level category
- How do you remove display of WooCommerce product category on single product page?
- Show Featured products in product category pages
- Show category images on single product page and product overview page
- woocommerce product category tabs [closed]
- Get Tags specific to Category using WooCommerce plugin
- How to change how many list item show in category generated by file edit-tags.php
- Show button only for certain Woocommerce Product Categories
- Target a specific category from a custom taxonomy?
- How to display product categories and number of sales on WooCommerce
- show category in woocommerce
- Unable to browser posts in categories
- Ajax Response Error
- List products from current category
- Display all subcategories for a product category in woocommerce
- Narrow Down a Shop Page Results Based on a Product Tag in WooCommerce
- Add product category to post_class
- Category archive rewrite rule to include category/subcategory?
- Category param redirect to post type archive
- How can I automatically display a visual archive of subcategory posts [and thumbnails] separate from Parent category?
- The default code for “posts_nav_link” on category.php isn’t working
- Read GET parameters in APACHE Mod Rewrite rules for WordPress categories in Posts
- batch adding posts to a category based on matching regex for title with SQL
- How to exclude children categories of parent category
- How to categorize posts under Category 1 and Category 2 which are separate lists of categories, and display them as separate lists in widgets
- Assign category from custom field on post creation and update
- Sticky posts not working when a specific category is set
- Place category Base URL at the end
- previous/next_post_link in the same sub-category?
- I want to display posts common in 3 categories [closed]
- Displaying just one new/current post per category in homepage
- Remove product category placeholder image (Woocomerce)
- Only one post is showing on category page, why?
- Get category url in loop
- Uncaught TypeError: jQuery(…).flipster is not a function in wordpress
- Have different permalinks based on category
- How do I output all category with custom field?
- Select posts by name and category per REST API
- Categories for each brands
- WordPress site periodically goes down
- How to hide category description in the subpages? [closed]
- Custom User-Created RSS Feed
- Post in multiple categories
- Why category.php throw 404 in wordpress while calling paginate_links()?
- How to get related tags based on a category?
- add_rewrite_endpoint returns 404 on archives
- Show category to visitors in different name or different language without changing basic category
- Huge category CSV import does not work
- Needing a snippet for listing category specific tags
- query cat orderby and order not working
- Order categories by name or view count
- Multiple category archive page
- Displaying Categories and Sub Categories
- How to assign posts without any category to the default category
- List subcategories of active parent category (WooCommerce)
- Remove sub-menu item from admin menu
- Redirecting thousands of posts that currently have no category in their permalink when changing permalink structure to include category
- WordPress Get Posts from each category equally
- Deactivating all plugins and reactivating one by one, fixes conflicts. Why?
- Show only first category name in RSS?
- Change thumbnail image depending product category
- Show parent category in URL of subcategory archive page
- Unwanted Duplicate Product Categories in WooCommerce
- How to organize custom categories for filtering and selecting
- how to get child category title and image
- How to display categories which contains particular tag?
- WP didn’t redirect to canonical category URL
- Woocommerce custom category pages
- Custom archive.php: retrieve right post categories?
- Custom sub taxonomy order by
- I cannot add more Categories to my menu
- Add category only if post has custom taxonomy category
- Customize WordPress widget – Product Category
- Paging not working in category.php
- Restrict author to create subcategory in a selected category
- Post injections into Site Origins Page builder [closed]
- Custom Post Type + Categories
- “Categories” for six authors [closed]
- Why do plugins deactivate by themselves?
- Customize permalink wordpress category id
- WordPress shows error related to allow_url_fopen
- WordPress/Woocommerce only show category in shop to a single role
- Previous/next post links displaying wrong category than is desired
- How to Add Default Content for all Category Pages Before and After Content?
- How to create a page which lists all categories with its ratings [closed]
- Add /category/ to an author archive page
- SQL to migrate post types to taxonomy
- How do I toggle pagination on/off in search results and category listings via a link?
- Mod_Rewrite to show only last subcategory in URL
- wp_dropdown_categories() scripts working erratically on sidebar
- Permalinks: display just one kind of wp category in the posts url
- Send to different single pages by category when multiple categories exist