Support

Account

Home Forums General Issues Category Color in Single Post

Solved

Category Color in Single Post

  • Hi,

    according to this tutorial
    https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
    I got it to work, so that my archive.php (https://pastebin.com/yCx8M8HP) (ENFOLD-Theme) shows the category image and post-title as well as the category name in the post-meta section in its category color.

    I added the code snippets from the tutorial in
    – line 12-16
    – line 39-51

    Shows like this:
    https://imgur.com/lC5SnHU

    So far, so good.

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    But now I need it in a single post view:
    in the post-meta section the category name should also show in its own color:

    https://imgur.com/APYdxsz

    But my PHP is too bad, so that I don’t know where to add these snippets into my loop-index.php (that is for the single posts of ENFOLD-Theme).

    Here is the file:
    https://pastebin.com/nafjw9LQ

    line 90-103 is where I tried to add the snippets according to the tutorial mentioned above:

    But I don’t know, if this

    //	get the current taxonomy term
    			$term = get_queried_object();

    is right on this file for single post, because it is not an archive/category page?

    and I don’t know, if this is the right place in the file to add these snippets:

    		//	get the current taxonomy term
    			$term = get_queried_object();
    		//	vars - ACF for showing ColorCoding of Category
    			$color = get_field('color');
    						
    ?>
    
    		<style type="text/css">
    			.blog-categories.minor-meta a {
    			color: <?php echo $color; ?>;
    			}
    		</style>
    
    <?php	

    Normally the output of my category name in the post-meta is in line 207 of this file:

    if(!empty($cats))
                        {
                            $cat_output .= '<span class="blog-categories minor-meta">';
                            $cat_output .= $cats;
                            $cat_output .= '</span>';
                            $cats = "";
                        }

    Long story short, all I need in this file (https://pastebin.com/nafjw9LQ) is to place:

    //	get the current taxonomy term
    			$term = get_queried_object();
    		//	vars - ACF for showing ColorCoding of Category
    			$color = get_field('color');

    … if this is right for a single post file? And:

    <style type="text/css">
      .blog-categories.minor-meta a {
      color: <?php echo $color; ?>;
      }
    </style>

    This works fine in the archive.php.

    Since I am not a coder could anyone please help me out where to add these snippets in correct PHP syntax? Thanks a lot, appreciate.

  • G’day Mate,

    the function get_queried_object is getting the current object. So, on a archive page, the current object is the current taxonomy term. However, on the single, it is the current post. That’s why acf couldn’t return the value unless you tell it to get it from the taxonomy.

    On line 192, where the theme is using get_the_term_list to build the category list. However, if you want to apply custom class to it, you’d need to construct the html output yourself.

    So, you should update that section to look like:

    
    if(!empty($taxonomies))
    {
        foreach($taxonomies as $taxonomy)
        {
            if(!in_array($taxonomy, $excluded_taxonomies))
            {
                // get all the terms in this taxonomy
                $terms = get_the_terms(null, $taxonomy)? : [];
    
                foreach ($terms as $term) {
                    // loop through them, and add the color as style attribute
                    $cats .= sprintf(
                        '<a href="%s" style="color: %s">%s</a>', 
                        get_term_link($term),
                        get_field('color', $term),
                        $term->name
                    );
                }
            }
        }
    }
    

    Cheers

  • OMG, that works like a charm. Thank you so, so much!!!!
    Just a last question:

    So I do not need these 2 lines

    //	get the current taxonomy term
    	$term = get_queried_object();

    here in the loop-index.php at line 90: https://pastebin.com/nafjw9LQ ?

  • No, you don’e need line 90 -> 101 cause they are not relevant on the single anymore 🙂

  • Thanks a lot, gummi, I really appreciate your time to help me, it works perfectly!!!!

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

The topic ‘Category Color in Single Post’ is closed to new replies.