![Check out Enfold](https://kriesi.at/wp-content/themes/kriesi/images/banner-enfold.jpg)
Tagged: categories, category, Color, colours
-
AuthorPosts
-
February 22, 2018 at 6:37 pm #916199
Hi,
how can I give each category name in the minor-meta an own color?
Thank you.
-
This topic was modified 6 years, 6 months ago by
BeeCee. Reason: added private login data
February 22, 2018 at 9:12 pm #916340———————- UPDATE ————————–
I partly managed it using the AdvancedCustomFields Plugin.
I created fields for the category image and the category color:
and added in archive.php according to the documentation here:
- Right after
get_header();
:// get the current taxonomy term $term = get_queried_object(); // vars - ACF for showing Images and Color in Category Backend $image = get_field('image', $term); $color = get_field('color', $term);
- and before the term-description this:
<style type="text/css"> .entry-content-wrapper .post-title { color: <?php echo $color; ?>; } .blog-categories.minor-meta a { color: <?php echo $color; ?>; } <?php if( $image ): ?> .categoryheaderimage { background-image: url(<?php echo $image['url']; ?>); } <?php endif; ?> </style> <div class="categoryheaderimage"> </div>
It works fine, it shows my category image and the post title in its category color as well as the category name in the minor-meta.
Here when editing a category:and the output:
————————-> Now my question:
I can’t get exactly these snippets to work in the loop-index.php as I had in the archive.php described above, so that the category name in the minor-meta is shown in its category color:I simply don’t know, where in the loop-index.php I can add this for grabbing the ACF fields:
(1)
// get the current taxonomy term $term = get_queried_object(); // vars - ACF field for showing Category Color in Category Backend $color = get_field('color', $term);
and where to add this HTML:
(2)
<style type="text/css"> .entry-content-wrapper .post-title { color: <?php echo $color; ?>; } .blog-categories.minor-meta a { color: <?php echo $color; ?>; } </style>
Maybe
get the current taxonomy term $term = get_queried_object();
is not right like in archive.php, because the loop-index.php is not a category page?!?
… if you could please just let me know, where in the loop-index.php I can add correctly these PHP syntax.
Thanks a lot.-
This reply was modified 6 years, 6 months ago by
BeeCee. Reason: added login data
February 24, 2018 at 1:09 am #916934— SOLVED —
The code in loop-index.php must be rewritten in some part:
Replace
if(!empty($taxonomies)) { foreach($taxonomies as $taxonomy) { if(!in_array($taxonomy, $excluded_taxonomies)) { $cats .= get_the_term_list($the_id, $taxonomy, '', ' ','').' '; } } }
with this
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 ); } } } }
At the ACF forum told me someone right now:
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.
In the part, where the theme is usingget_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.Result:
the category color is grabbed from the color field in the AdvancedCustomFields plugin, which makes an extra colorpicker in the category editing panel:Output Single Post:
Replace exactly the same lines with the same code snippet in the postslider.php, so that the category have its own color, too, when you are using the ALB blog posts element.
All is fine, you can close the ticket now.
February 24, 2018 at 4:40 am #916957Hi,
Thanks for sharing. I’m sure it will help a lot of users. :)
Best regards,
IsmaelJune 4, 2018 at 2:40 pm #966522so far, so good, worked fine until now. But do I have the possibility, to modifiy the code
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 ); } } } }
in that way, that I can add a prefix in front of the taxonomy (category)?
It should look like:
but the prefix is not linked to the category like the category term itself.In WP docu I found
<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
But how can I add a “before”, a prefix in front of the category, that is linked to the category, too (important!) using my code snippet above?
Thanks.
June 6, 2018 at 6:55 am #967665Hi,
Edit this code.
$cats .= sprintf( '<a href="%s" style="color: %s">%s</a>', get_term_link($term), get_field('color', $term), $term->name );
Include the prefix.
<a href="%s" style="color: %s">Prefix here: %s</a>
Best regards,
IsmaelOctober 18, 2018 at 7:37 pm #1023592Just would like to let you know, that there is a much easier way to give each category its individual color as I described above ;-)
Found a newer script from Ismael, that I added to the “Code-Snippets” plugin and it works perfectly and easy (thanks, Ismael, for that):
October 21, 2018 at 6:18 pm #1024544Hi Gitte,
Glad you got it working for you and thank you for sharing! :)
If you need further assistance please let us know.
Best regards,
Victoria -
This topic was modified 6 years, 6 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.