Skip Navigation

[Resolved] Get value of a custom field

This support ticket is created 5 years, 2 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by stephanF 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1555217

Tell us what you are trying to do?
To format categories, I would like to get a custom field associated to each category.
I've create the field using Toolset.
I've already a shortcode ready BUT I don't known how to get the value of the custom fiels

#1555325

For information , I would like to use a php / shortcode to do this .

#1555339

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stephan,

Thank you for getting in touch.

Are you using a view to display the taxonomies? If so then you can use our Types shortcode to display the field information.

Example for custom field for taxonomy would be.

[types termmeta="my-text-field"][/types]

Where my-text-field is the slug of the custom field.

Please let me know if this helps.

Thanks,
Shane

#1555369

OK ... This will not work. Let me be more precise.

- Each post has many categories associated with.
- I ve created a custom field (categoriecolor) which is attached to categories
- For each category , I ve filled different color value.

My goal is:
When displaying an article nor an archive, I would like to display the categories with the right color.

By the past I was using ACF and the php API to do this, but today I would like to use only Toolset to do that.

Here my php code in functions.php
In the function style_taxonomies_color, you will see the function(get_field) I use for ACF version, which is the Toolset equivalent

...

function style_taxonomies_color($category)
{
//
// get_field is an ACF function
//
$topic_color = get_field('categorie_color',$category->taxonomy . '_' . $category->term_id);

if ($topic_color == "")
$topic_color="black";
$html= '<span style="color:'.$topic_color.'">'.$category->name.'</span>';
return $html;
}

function custom_taxonomies_color($atts=[],$tag=[]){
global $post, $post_id;
// get post by post id
// $post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);

$wporg_atts = shortcode_atts(['cat' => 'article'], $atts, $tag);
$taxname = $wporg_atts['cat'];
foreach ($taxonomies as $taxonomy)
{
if ($taxonomy == $taxname)
{
$terms = get_the_terms( $post->ID, $taxonomy );
$html = style_taxonomies_color($terms[0]);
$link = get_term_link($terms[0]->slug, $taxonomy);
$html = "".$html."";
return $html;
}
}
}
add_shortcode("displayStyleCategorie","custom_taxonomies_color");
....

Shortcode Usage in a View :
<div class="categorie">[displayStyleCategorie cat='mediakwest_cat']</div>

FIll free to connect on my test site if I'm not clear.

Regards
Stephan

#1555377

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stephan,

Thank you for the clarification. We do have an API for this

In you case the PHP API for the taxonomy fields would be

types_render_termmeta( "my-text-field", array() );

Take a further detailed look at the possible parameters from this example code in the link below.
https://toolset.com/documentation/customizing-sites-using-php/functions/#textfield

This contains an example for all our custom field types.

Please let me know if this helps.
Thanks,
Shane

#1555609

Hi Shane,
After some research in the documentation I found the right coding way:

$topic_color = types_render_termmeta( 'categoriecolor', array('term_id' => $category->term_id ,'output' => 'raw'));

Thanks for your help.

Just a quick remark, the php documentation is little bit fuzzy for me and some real examples would be great.
Anyway,
Thanks again
Stephan

#1555611

My issue is resolved now. Thank you!