We create in an WooCommerce Shop products wich are assigned to different product-categories. Also these products are assigned to a custom taxonomy created with types called "producer" (microsoft, autodesk a.s.o.).
The problem is, that we need to show in the producer archive page where all products are shown wich are assigned to the producers, also a list of links to all product-categories wich the products of the assigned to?
Is that possible?
Is the intention that you show one list of product categories at the top of the archive which are connected to any of the products that appear in the archive, or do you show a list for each individual product in the archive of the product categories for that individual product?
we try to
A list of products wich are assigned to the custom taxonomy - and at the top of thes a list of productcategories which are connected to any of the products that appear in the list
F.e..
All Products from Producer Microsoft
These Products are assigned to product-categories: Word | Excel | PowerPoint
1. Microsoft Word for Beginners
2. Microsoft excelfor Beginners
3. Microsoft Office 365 for Beginners
4. Microsoft Office 365 for Professionals
You can find the list of IDs of posts appearing in this archive on the global $wp_query object, and you can use that to query the terms of the producer taxonomy assigned to those posts, then output their names.
Writing such code for you is outside our support policy, but you are in luck, it is fairly quiet at the moment so I put together an example you can use:
add_shortcode('producers', function () {
global $wp_query;
$obj = $wp_query->get_queried_object();
// extract the IDs of posts found on this archive
$post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
// Get the terms of producer taxonomy assigned to posts in this archive
$term_names = get_terms( array(
'taxonomy' => 'producer',
'object_ids' => $post_ids,
'fields' => 'names'
));
return implode( ", ", $term_names);
});
You might need to modify it, particularly the slug of your taxonomy, and if you want to output anything other than a comma-separated list of names which is what I've done, but I will leave that to you.
Hi Nigel, tnx for your help :-))
I place the short code in the functions.php of the theme.
Short Questions:
i try to show the term link of the categories instead of the names only.
1. Was ist correct to change line 17 to return implode( ", ", $term_link); ?
2. I check the toolset documentations to find out how to use these shortcode now to produce a view wich generates the list of Product-Categories assigned to products from producer - but i didn'nt find the right way to handle it?
The shortcode uses get_terms to retrieve the relevant taxonomy terms, and the fields argument is set to return these terms as an array of term names (which the final line then converts into a comma-separated list).
You can only return things like the term IDs, term slugs, term names etc., not as links.
So you would likely want to modify the code so that it returned IDs instead of names and then you would use a foreach loop to iterate over these and use get_term_link() to get the link for each term.
(Shortcodes must return a string, so you would start with an empty string, e.g. $output="";, and then append each link to that string before returning it.)
I just noticed, re-reading your question that you are adding this shortcode to the producer archive and it should display product categories, I had thought you were doing it the other way around, so you would need to change the taxonomy argument to 'product_cat' which is the slug of the product categories taxonomy from WooCommerce.
In terms of where you use this shortcode you would insert it somewhere in the output section of a custom archive for the producer taxonomy (which you can make at Toolset > WordPress Archives if you haven't already).