You are overwriting $product_cat_slugs
on every iteration of your loop – so when it’s finished, that variable will be set to the last value.
Why not do all of your outputs inside that loop?
Or if for some reason you don’t want to do that, make $product_cat_slugs
an array instead of a variable:
$product_cat_slugs = [];
foreach ($terms as $term) {
$product_cat_slug = $term->slug;
$product_cat_slugs[] = ' product_cat-' . $product_cat_slug;
}
Related Posts:
- List all categories but exclude current post category on single post page
- Problems with function on function.php
- Include Parent Term in wp_list_categories
- Get all categories post is not in
- WordPress custom taxonomy
- How do I edit the terms output args or array data?
- Create category after theme setup and modify the default one
- WordPress using get_term to retreive slug not working as expected
- What does the token %1$s in WordPress represent [closed]
- exclude multiple terms using get_terms() function
- Archive listing of posts by publish year (multiple years)
- Add custom field to attachments in a specific category
- Display all posts in current category
- category not display in word press grammatically
- Looking to exclude blog posts from category Previous/Next buttons
- How to display related posts from parent category
- How can I set custom order use this function
- How do I get taxonomy terms by ID in a specific order
- WP_Query use for a filter with multiple Taxonomies and Terms
- Put a link to a category round a hard coded A HREF
- Check if Product is in a Specific Category in Functions.php
- get_the_terms – only top level
- Category Thumbnail Display – How to display ONLY the main Category
- combine Code 1 with Code 2
- Creating a related post section based on similar categories
- Swapping wp_dropdown_categories function with wp_category_checklist
- Issue adding sub category programmatically
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- Grab posts by multiple categories
- get_template_part based upon post’s category
- how to save selected option in variable for rest api category filter
- Recent Posts Not Showing Only On A Specific Category Page [closed]
- How to get post’s current parent term ID?
- Display category name only once inside loop
- Insert term and child term from frontend
- how to show only specific category for a template
- Creating sub-categories via php
- Define specific category name in PHP
- Edit the_category (); for a hierarchical display
- Category title output before opening title tag
- How can I access string value in an array?
- How to array only one key from another array
- I’m trying to create an array with a foreach loop, but the array only stores the last item [closed]
- Displaying posts based on category
- How do I remove a category from a wordpress loop>
- Get categories names as an array to use it in theme settings
- How to get the last category name of a child category?
- Hierarchical taxonomy list with modificated term links
- How to separate categories with commas?
- Exclude product attributes from taxonomy terms loop
- How to hide posts of a specific custom category in WordPress?
- Show all tags within a category?
- Add new post in existing categories using wp_insert_post
- How to change logo by category
- Problem with custom loop and wp_list_pluck [closed]
- Alphabetically Ordered Category in WordPress
- Output category list inside array
- Loading Posts & Category with Ajax
- How to get current category
- WooCommerce: Display product categories to make IF statement
- Trying To Get Child Categories To Show, Issue With foreach Loop
- Define category ID using get_post_meta
- Show parent categories of the current category
- How to check if a category has a parent and child categories?
- How to break up php code to avoid echo
- Get category id when SEO URL is turned on
- Trying to display terms from custom taxonomy within function
- Display latest post from WordPress Featured Category that is also in X,Y,or Z categories
- Display Sibling WooCommerce Product Categories on Archive Page
- display the months of a specific category
- I need to display posts in subcategory beside posts in main category
- Add Custom Category Description When Empty
- Order category posts by last word of custom field
- Add a product category to a specific product via a button
- List sub categories and corresponding posts of a parent category
- Dropdown category field inside repeatable metabox
- Get nice name of category from slug (remove dashes of category)
- How to Link to category titles if it is in the single post content?
- How can i show specific Category List?
- How to show category just on specific pages?
- wp_set_object_terms returns 500 error when called through AJAX function
- How to filter specific element of an array in wordpress/php?
- All categories options or All categories not Populating
- PHP get_category() function redeclared
- List categories assigned to a post
- Unable to get Post Category Name and URL
- How to add style in functions.php depending on conditions?
- How do I display Category titles on a WP Page?
- How to display terms from two taxonomies in one php code?
- problem with category menu, it doesn’t want to show link
- Saving category as insert post
- Trying to see if page is category or single and displaying title with appropriate heading tag
- Get all the contents of taxonomy and sort by term
- get_category only returning details for ‘uncategorized’
- Add Column to Term Database
- category & sub category order issues in wordpress
- Fix incorrect related posts code snippet
- Display a list of posts corresponding to a category
- How to create categories out of a list of words?
- Is there a way of displaying related posts from lowest hyrachical order (lowest child category to parent category)