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