Skip Navigation

[Resolved] Display custom term fields somewhere besides term archive or term view

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to show custom taxonomy term fields in a WooCommerce Products Archive (or anywhere outside of a Term Archive or a Term View).

Solution: Use PHP and the Types Fields API to create your own custom shortcode. This shortcode can be used to display custom term metadata. A simple implementation would look like this:

add_shortcode( 'display_termmeta_field_info', 'display_termmeta_field_info_func');
function display_termmeta_field_info_func($atts)
{
  $slug = $atts['slug'];
  return types_render_termmeta( $slug , array() );
}

Then you may use the shortcode like so:

[display_termmeta_field_info slug="my-term-slug"]

Relevant Documentation: https://toolset.com/documentation/customizing-sites-using-php/

This support ticket is created 7 years, 5 months ago. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by fabianT 7 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#511621

I am trying to show term fields in my woocommerce products archive. I am using different taxonomies for my products: Spielort (Location), Bundesland (Country). These taxonomies have custom fields like a description, an image and the address of the location.

I am using these in my taxonomy archive loop and it works fine:

<h4>[wpv-post-taxonomy type="spielort"]</h4>
	<p>[types termmeta='strasse-spielort'][/types]<br>
	[types termmeta='plz-spielort'][/types] [types termmeta='ort-spielort'][/types]
     </p>

Now I need a way to also show them in my product archive loop. When I am using this code, it just shows nothing. Your documentation (https://toolset.com/documentation/user-guides/displaying-wordpress-term-fields/) says "At the moment, you can display term fields on a View listing terms and on a term archive page.".

I really need some way to show the termmeta on different pages. Can you show me a workaround to do this? There has to be some way to get them via PHP, but I can't figure it out.

Thanks in advance.

Kind regards
Theo

#511733

Hi, the same document you linked to contains some information about how to display these terms using PHP. For example, let's say 'strasse-spielort' is the slug for a textfield. You could use this code to output the strasse-spielort field:

types_render_termmeta( "strasse-spielort", array() );

The second parameter, array(), can include any attributes you want to pass into the Types API. The available attributes for each type of field are defined in the API documentation here:
https://toolset.com/documentation/customizing-sites-using-php/functions/
You can also see some PHP examples for each field type (click the + More orange link).

If you want to be able to display these values in your Content Templates or Views, where you can't use raw PHP, you can create custom shortcodes that call these PHP functions for you. Here's a very simple implementation of that concept:

add_shortcode( 'display_termmeta_field_info', 'display_termmeta_field_info_func');
function display_termmeta_field_info_func($atts)
{
  $slug = $atts['slug'];
  return types_render_termmeta( $slug , array() );
}

In your Content Template or Page or Post, etc:

[display_termmeta_field_info slug="strasse-spielort"]

More information about creating your own custom shortcodes can be found here:
https://codex.wordpress.org/Shortcode_API

If there is a specific step in the process you're having trouble with, feel free to reach out here and I can try to assist. Let me know how far you can get, and where you are stuck.

#512320
FireShot Capture 11 - Baker Street Saarbrücken – Erlebnisdin_ - http___relaunch.erlebnisdinner.de_ Kopie.jpg

Dear Christian,
thank you very much. This was very helpful. But now I am getting to a different problem:
I am using the taxonomy "Spielort" (Location) to give my products (event tickets) a location structure. For example:
Saarland -> Baker Street Criminal Tearoom & English pub
Saarland -> Hotel xyz
Saarland -> and so on ...
Rheinland-Pfalz -> Hotel abc
Rheinland-Pfalz -> and so on ...
...

This archive template will list all products in 'saarland' or in a specific location like 'Baker Street'. The product list also contains the address, which comes from the attached 'spielort' (location) in the product loop. The address also comes from the custom term fields in 'spielort' 2nd level ('Baker Street').

I just realize, it is hard to explain, without showing. Could you please set my next response to private, so I could provide you some credentials to show you what it should look like. Enclosed you will find a screenshot.
Thanks in advance

Kind regards
Theo

#512324

Hi, I'll be glad to take a look at that - may I kindly ask you to open a separate forum ticket? Since this is a separate issue, I would prefer to address it independently. This keeps our forum organized so it's easier for other users to search for answers to their problems.

Feel free to assign the ticket directly to me and I will begin as soon as I can. Thank you!

#512368

How I can ein create a new ticket and assign it to you?
First, you mark this ticket as resolved. Then, when you create a new ticket, the system will offer you the option to assign that ticket directly to the supporter who worked on your most recent ticket. There aren't many tickets in the queue right now, so if you have trouble assigning to me, just create the ticket and I will assign it to myself. I'll watch the queue for a ticket from your profile.

#512632

Thanks!

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