Skip Navigation

[Résolu] View wich shows Categories for Products wich are assigned to a custom Taxonomy

This support ticket is created Il y a 4 années et 11 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Marqué : ,

This topic contains 5 réponses, has 2 voix.

Last updated by Nigel Il y a 4 années et 11 mois.

Assisted by: Nigel.

Auteur
Publications
#1242038

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?

#1242043

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Thomas

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?

#1242048

Hi Nigel,

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

#1242081

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Thomas

You would need a custom shortcode for this.

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.

#1242258

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?

#1242896

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Thomas

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.)

https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#parameters
https://developer.wordpress.org/reference/functions/get_term_link/

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).

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.