Support

Account

Home Forums Front-end Issues Not getting color for category labels using custom taxonomy

Solved

Not getting color for category labels using custom taxonomy

  • I have tried to implement this https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    This is my current code wich returns no color whatsoever.

    <ul>				
    <?php
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;				
    $term_id = $queried_object->term_id;  
    $color = get_field('color_de_fondo_categoria_tienda', $taxonomy . '_' . $term_id);	
    
    $cat_array = ( get_the_terms( get_the_ID(), 'categorias_tienda') );
    	foreach ( $cat_array as $cat  ) {
            if($cat->name != 'Destacados') { // the term ID you want to exclude
                echo '<li style="color:' . $color .';">' . $cat->name . '</li>';
            }
         }
     ?>
     </ul>

    do note it is in a regular page template such as “index”.

  • What you are trying to do will only work on a term archive page.

    If index.php is showing the main archive, this is not a term archive and get_queried_object() is returning an object of “Post_Type”

    You would need to get the field from the category inside of your loop over the categories using $cat->term_id from your example code.

  • Excellent works perfectly, thank you so much!!!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.