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!
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.
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);
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/