Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #916199

    Hi,

    how can I give each category name in the minor-meta an own color?

    View post on imgur.com

    Thank you.

    • This topic was modified 6 years, 10 months ago by BeeCee. Reason: added private login data
    #916340

    ———————- UPDATE ————————–

    I partly managed it using the AdvancedCustomFields Plugin.

    I created fields for the category image and the category color:

    View post on imgur.com

    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:

    View post on imgur.com

    and the output:

    View post on imgur.com

    ————————-> 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:

    View post on imgur.com

    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, 10 months ago by BeeCee. Reason: added login data
    #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 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.

    Result:
    the category color is grabbed from the color field in the AdvancedCustomFields plugin, which makes an extra colorpicker in the category editing panel:

    View post on imgur.com

    Output Single Post:

    View post on imgur.com

    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.

    #916957

    Hi,

    Thanks for sharing. I’m sure it will help a lot of users. :)

    Best regards,
    Ismael

    #966522

    @Ismael

    so 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:

    View post on imgur.com


    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.

    #967665

    Hi,

    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,
    Ismael

    #1023592

    Just 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):

    #1024544

    Hi 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

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.