I am trying to get the category slugs & their custom taxonomy that i created for each product in the loop. The problem I am having is getting the custom taxonomy’s slug.
I have tried altering wp_get_post_terms like this $product_cats = wp_get_post_terms( get_the_ID(), array(‘product_cat’, ‘workplaces’ );
I am not sure how i can get the associated category AND workplace with a particular product
I need it so that when a person clicks on the category, it will link to the category archive and show products specific to the workplaces taxonomy.
The workplaces tax is hierarhical, so that means some products will be associated with sub workplaces, but still will be displayed in the parent workplace
i am thinking it is an associative array that i need, but i am not sure.
sorry if this is confusing.
if ( wc_get_loop_prop( 'total' ) ) {
$category_slugs = [];
while ( have_posts() ) {
the_post();
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
foreach( $product_cats as $collection ) {
$category_slugs[] = $collection->slug;
}
?><pre>
<?php $category_slugs = array_unique($category_slugs);
print_r($category_slugs); ?>
</pre>
<?php
/*custom tax code */
function taxonomy_workplaces() {
$labels = array(
'name' => _x( 'Workplaces', 'taxonomy general name' ),
'singular_name' => _x( 'Workplaces', 'taxonomy singular name' ),
'search_items' => __( 'Search Workplaces' ),
'all_items' => __( 'All Workplaces' ),
'parent_item' => __( 'Parent Workplace' ),
'parent_item_colon' => __( 'Parent Workplace:' ),
'edit_item' => __( 'Edit Workplace' ),
'update_item' => __( 'Update Workplace' ),
'add_new_item' => __( 'Add New Workplace' ),
'new_item_name' => __( 'New Workplace Name' ),
'menu_name' => __( 'Workplaces' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'workplace' ],
);
register_taxonomy( 'workplaces', [ 'product' ], $args );
}
add_action( 'init', 'taxonomy_workplaces' );