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
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- Grab posts by multiple categories
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to show single category archive
- Move category description below post list in blog
- How to pick the default selected value in wordpress dropdown?
- Applying A Category to Existing Posts Where Page Title Matches Regex
- Get page that displays all children of taxonomy parent
- How to get original custom taxonomy slug after the slug has been rewritten?
- selected option if current category is the value
- get_template_part based upon post’s category
- how to save selected option in variable for rest api category filter
- Setting default category base on theme activation
- Recent Posts Not Showing Only On A Specific Category Page [closed]
- Hide subcategories (widget)
- Get term siblings of current child taxonomy
- Alert Bar section within WP loop is displaying even though there are no posts
- Let Users Choose Post Categories
- Different post styles depending on category
- 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 subcategories in parent category not parent category?
- how to show only specific category for a template
- Dilemma of Populating all the categories in a drop down list
- Display most popular posts of category
- Creating sub-categories via php
- Define specific category name in PHP
- Edit the_category (); for a hierarchical display
- JSON REST API WordPress only showing first 10 categories
- Category title output before opening title tag
- List taxonomy term slugs within shortcode (do_shortcode)
- get taxonomy thumbnail and use it as a variable in code
- How can I access string value in an array?
- Copy taxonomy terms from one post to another programmatically
- 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]
- Limit Taxonomy Output in Conditional Statement
- How can I display a div ONLY if it is not a certain term page
- Get latest post from all categories except one
- Hide image if taxonomy term is empty
- Displaying posts based on category
- How do I remove a category from a wordpress loop>
- Isotope Drop Down Categories Filter Function
- Get categories names as an array to use it in theme settings
- How to filter custom taxonomy term name, slug, and description?
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- How to get the last category name of a child category?
- Hierarchical taxonomy list with modificated term links
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- Use get() method to grab all categories and output inside another method
- Only show size attributes in product box woocommerce when available
- How to get category name or ID by post?
- Custom post type category not displaying category ID
- Argument for if term-> have child?
- Advice on creating a WP Archive Template with Custom Taxonomy (get_term_meta)
- How to get current post category details inside “loop”?
- 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?
- Post not populating for custom post type based on category selection
- 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]
- Listing titles and custom fields of entries within subcategories
- random display categories – change url
- Displaying list of posts in category page
- Echoing a CSS class based on category of post in a list
- Alphabetically Ordered Category in WordPress
- Output category list inside array
- Do not duplicate posts with multiple categories in multiple loops
- Loading Posts & Category with Ajax
- Removing sidebar on category pages
- How to get current category
- WooCommerce: Display product categories to make IF statement
- Trying To Get Child Categories To Show, Issue With foreach Loop
- Create a a form for custom taxonomy terms
- Define category ID using get_post_meta
- List posts by their custom taxonomy term, with a condition
- Show parent categories of the current category
- is user member of a group, show them categories [closed]
- Showing Subcategory Name/Link Instead of Parent Category
- How to check if a category has a parent and child categories?
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- How to break up php code to avoid echo
- Woocommerce getting top level category parent and make all sub categories have the same template and menu
- go to home page when i select default in select-box
- Finding and removing duplicates within WP Arrays