Skip Navigation

[Resolved] Displaying taxonomy term on front end

This support ticket is created 4 years, 4 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.

Our next available supporter will start replying to tickets in about 0.94 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 4 years, 4 months ago.

Assisted by: Jamal.

Author
Posts
#1687095

Hello - I'm trying to display a taxonomy term on the front end via my functions.php

I'm trying to find out the equivalent of this code which defines the custom field (business name)

$biz = types_render_field("business-name");

Trying to do the same for getting the taxonomy. The tax slug I'm trying to pull is va-cat and I'm using this, but no luck - can you tell me the correct command for this?

$vcat = types_render_termmeta("va-cat");

Thanks!

#1687471

Hello and thank you for contacting the Toolset support.

The following will only work on a term archive page, or in a taxonomy view inside the loop. Because, that way, it will pick the term from a global variable:

$vcat = types_render_termmeta("va-cat");

Otherwise, you will have to pass the term_id to the function:

$vcat = types_render_termmeta( "va-cat", array( 'term_id' => $term_id ) );

Check the example codes in this documentation https://toolset.com/documentation/customizing-sites-using-php/functions/

I hope this helps. Otherwise, please provide more details about your code and where it is being used.

#1689313

Thanks for your help. I can't get it to work.

This code is being placed in my functions.php to replace the default data that is populated from a plugin. The fields work fine, but i can't get the category to work. It's not returning anything. I can send you the link to the dev site privately if you'd like to see it.

function cspml_custom_item_description($default_content, $post_id){

$biz = types_render_field("business-name");
$city = types_render_field("city-state");
$vcat = types_render_termmeta( "va-cat", array( 'term_id' => $term_id ) );

return '<h5 class="biz-name" style="margin-bottom: 0">'.$biz.'</h5><div class="city">'.$city.'</div><div style="vcat'.$vcat.'">'.$vcat.'</div>'

;

}
add_filter('cspml_item_description', 'cspml_custom_item_description', 10, 2);

#1689787

As you can see, the hook will pass to arguments to your function, $default_content and $post_id

function cspml_custom_item_description($default_content, $post_id){

You will have to get the $term_id from the $post_id, or probably multiple $term_ids if the post is assigned multiple terms. You will use the wp_get_post_terms:

$term_ids = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'ids' ) );

- https://developer.wordpress.org/reference/functions/wp_get_post_terms/

It will return an array of terms assigned to the post. You can pull the first one with $term_ids[0], or you can go through all of them with foreach
Then you can use types_render_termmeta.

Please note, that this is custom code, and is out of the scope of our support forum. If you are not comfortable with PHP coding, consider hiring a developer or one of our contractors https://toolset.com/contractors/