Skip Navigation

[Resolved] Displaying non-Types term fields on the term archive page with Blocks

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

Problem: I would like to display some non-Types termmeta fields in my Blocks term archive page.

Solution: This would require a custom shortcode solution:

function archive_term_meta_func($atts) {
  $a = shortcode_atts( array(
      'slug' => ''
  ), $atts );
  $term_id = isset(get_queried_object()->term_id) ? get_queried_object()->term_id : null;
  if( $term_id && $a['slug'] ) {
    return get_term_meta( $term_id, $a['slug'], true);
  }
  return;
}
add_shortcode("archive-term-meta", 
"archive_term_meta_func");

Then use it like this in an archive:

[archive-term-meta slug="some-slug"]
This support ticket is created 4 years, 8 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.

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 8 replies, has 2 voices.

Last updated by romanB-3 4 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1573849

Hi,
With Blocks, how may I add term fields to my term archive page ?
Thank you.

#1574853

Hi, as far as I know taxonomy term fields aren't supported in either the single field block or the fields and text block, but I will double check with my 2nd tier support team. User and term fields and Views were not included in the first round of Blocks, so the best way to display these fields is to use the classic Types field termmeta shortcodes, for example:

[types termmeta="term-wysiwyg-field-slug"][/types]

You can place termmeta shortcodes in the archive output, either inside or outside of the archive loop.

Please stand by and I will let you know if my 2nd tier support team has some feedback on this.

#1575923

Hello and thank you,
I've tried this within my Archive page applied on single terms :

<h1>[types termmeta="term-wysiwyg-field-name"][/types]</h1>
<h1>[types termmeta="term-field-name"][/types]</h1>
<h1>[types termmeta="term-name"][/types]</h1>
<h1>[types termmeta="name"][/types]</h1>
<br>
[wpv-filter-meta-html]
[wpv-layout-meta-html]

But none of the h1 shows up...

#1576349

The code you shared here looks okay. This code is from a WordPress Archive, created in the Classic Editor, right? That should work:

<h1>[types termmeta="term-wysiwyg-field-name"][/types]</h1>

Assuming the slug of the term field is term-wysiwyg-field-name, the contents of this field should appear . If that is not the case, I will be glad to take a closer look. Can you provide login credentials?

#1577087
    <h1 class="">[wpv-taxonomy-title]</h1>
    <p class="">[wpv-taxonomy-description]</p>

Those worked ok for title and description.

    <p class="">[types termmeta="email-auteur"][/types]</p>
    <p class="">[types termmeta="site-officiel-auteur"][/types]</p>

And those also for custom fields.
But now I need to get custom fields not managed by types but this doesn't work :

sur_spip : <a href="[types termmeta='old_url']">[types termmeta='old_id'][/types]</a>

Thank you.

#1577645

I don't think there's an easy way to display non-Types custom term fields with any Toolset shortcode. You would need a custom shortcode that uses get_term_meta. Something like this recommended by Minesh: https://toolset.com/forums/topic/custom-shortcode-for-term-meta-data/#post-1116401

#1577815

Thank you.
I've tried to adapt Minesh's code without success.

function func_get_term_meta_data($atts) {
 global $post;
	$term_ids = wp_get_object_terms( $post->ID, 'auteur', array( "fields" => "ids" ) );
	if(is_array($term_ids)) {
		// $locations = get_term_meta($term_ids[0]);
		$locations=evo_get_term_meta('auteur', $term_ids[0]);
		return $locations['old_id'];
	}
}
add_shortcode( 'show_term_meta', 'func_get_term_meta_data' );

Should display the meta "old_id" of the term of the taxonomy "auteur".
But on this page I got this : hidden link
"Call to undefined function evo_get_term_meta()"
Thank you.

#1578609

Yeah it needs a lot of adaptation. Here's a more complete example:

function archive_term_meta_func($atts) {
  $a = shortcode_atts( array(
      'slug' => ''
  ), $atts );
  $term_id = isset(get_queried_object()->term_id) ? get_queried_object()->term_id : null;
  if( $term_id && $a['slug'] ) {
    return get_term_meta( $term_id, $a['slug'], true);
  }
  return;
}
add_shortcode("archive-term-meta", "archive_term_meta_func");

Then use it like this in an archive:

[archive-term-meta slug="some-slug"]
#1578833

Amazing support, thank you very much !
My issue is resolved now. Thank you!