Site logo

[Resolved] Change a color on the post based on category selected

Home Forums Support [Resolved] Change a color on the post based on category selected

Home Forums Support Change a color on the post based on category selected

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2422209
    John

    Hi, I have a custom post type for “Providers”, and a taxonomy for different categories of providers (only 1 is possible).

    With Metabox, I added a color custom field to my category.

    With GB Pro, I’m building a post template for the Providers custom post type. I would like to use the color of the provider category to color buttons and other elements of the template. Is that possible, without creating a new template for each category selection of that custom post type?

    I hope that makes sense.
    Thank you!

    #2422648
    David
    Staff
    Customer Support

    Hi there,

    my approach would be to:

    1. Save a global color for your eg.--provider-accent in Customizer > Colors.
    2. Build your template and use that color where required.

    GP will print this CSS:

    
    :root {
        --contrast: #222222;
        --contrast-2: #575760;
        --contrast-3: #b2b2be;
        ------- other variables here 00000
        --provider-accent: #ff0000;
    }

    3. Now we just need to retrieve the custom category value for the current post, and overwrite the CSS variable.

    So question ( i am not overly familiar with metabox ) do you know how to get the category field value from the current post ?

    #2423193
    John

    Thank you David for your response.

    I was exploring that same route so I have already defined the global colors via the customizer. Here is what I have for now, to make it more concrete:

    Post type: Licensee
    Licensee Categories:
    – Provider
    – App

    And I have created a Licensee #1, with the category of Provider, and Licensee #2, with the category of App.

    I want to create only 1 page template that Licensee #1 and Licensee #2 use, but I want several design elements to change color based on the Licensee Category selected:
    – Provider: blue
    – App: green
    (and other categories for other Licensee pages in the future)

    When I create the template, I have to choose a color for, say, a button. I’d like that color to be dynamically selected based on the Licensee Category, but in the GB settings I can only choose 1 global color for the button color.

    So as per your suggestion, I’m at step #3 but I don’t know how to overwrite the CSS based on the category selected – I assume I need some JS.

    To your question: metabox creates custom fields like ACF, I think it works very similarly. I know how to use GB dynamic data to get meta data or term data from the Post Type, but not from the Taxonomy Term associated with the Post Type. That’s what I was hoping to achieve; I created a custom field for the Taxonomy Term with its corresponding color code. But I don’t know how to retrieve it.

    #2423208
    John

    PS: Metabox has a “View” extension to build a template and output the custom field values on the front end. But I’m trying to use GP/GB exclusively for rendering the front end with dynamic data.

    #2423917
    David
    Staff
    Customer Support

    When I create the template, I have to choose a color for, say, a button. I’d like that color to be dynamically selected based on the Licensee Category, but in the GB settings I can only choose 1 global color for the button color.

    Correct. In the template you can only define a single global color.
    Theres no changing that – and neither would you want or need to 🙂

    We just need a mechanism that changes the global colors value based on what Category term the post type is.

    A simple solution is to add a CSS Class to the body tag that matches the current post taxonomy term.
    We can then use that Class to write in our new color variables.

    1. Add this PHP Snippet to add your CPTs custom taxonomy term to the body.
    Make sure the is_singular is correct and the your_custom_tax_slug matches your taxonomy.

    
    function db_cpt_term_body_class( $classes ) {
        // Set logic for post type - make sure the provider is the correct slug
        if ( is_singular('licensee') ) {
            global $post;
            $cpt_terms = get_the_terms( $post->ID, 'your_custom_tax_slug' );
            //Change 'your_custom_tax_slug' to your Custom Taxonomy slug.
            if ( $cpt_terms && ! is_wp_error( $cpt_terms ) ) {
                foreach ( $cpt_terms as $term ) {
                    $classes[] = $term->slug;
                }
            }
            return $classes;
        }
    }
    add_filter( 'body_class', 'db_cpt_term_body_class' );

    Now in our body tag on the single post we should see ( by inspecting the front end HTML ) our tax term. eg. provider or app

    2. Now in your Global colors.
    Creata a generic global color eg.

    --licensee-accent: #ff0000;

    This will be the color you set in the Content Template and can be the fallback color.

    Then add colors for the different terms:

    --provider-accent: #00ff00;
    --app-accent: #0000ff;
    --another-term-accent: #0f0f0f;
    

    3. Now it just requires some CSS, that uses our new body class selector to change the color:

    
    body.provider {
         --licensee-accent: var(--provider-accent);
    }
    body.app {
         --licensee-accent: var(--app-accent);
    }
    body.another-term-accent {
         --licensee-accent: var(--another-term-accent);
    }
    

    This method means you just need to add a new color ( in step 2 ) and write the CSS Rule ( in step 3 ) each time you add a new term.

    If in the future you want to switch up the colors for one of those terms, you just need to change its color in the Customizer > Global Colors.

    #2426157
    John

    Thank you David, that did it! I was missing the PHP snippet piece, now it works. I like your solution using the Customizer for setting the colors, which makes it easy for my other users to edit.

    Small correction for anyone reading this in the future: the CSS variable you’re defining via custom CSS is --licensee-accent not --provider-accent. So your custom CSS should be as follows:

    body.provider {
         --licensee-accent: var(--provider-accent);
    }
    body.app {
         --licensee-accent: var(--app-accent);
    }
    body.another-term-accent {
         --licensee-accent: var(--another-term-accent);
    }
    #2427087
    David
    Staff
    Customer Support

    Glad to be of help. And for my own sanity and OCD i corrected the CSS above 🙂

    #2503469
    John

    Hello David,

    How would I go about modifying the script you shared above, if I wanted instead to add a class to the body of the page for each taxonomy term of that taxonomy?

    So instead of adding the class to a custom post based on the term it is tagged with, I want to add the same class to the term archive page for that taxonomy. Is that possible?

    #2503529
    Ying
    Staff
    Customer Support

    I want to add the same class to the term archive page for that taxonomy.

    If it’s a term archive, it has the class already.

    #2503616
    John

    That would be great, but that’s not the case. Here is the body element opening and its classes

    <body class="no-sidebar nav-below-header separate-containers header-aligned-left dropdown-hover customize-support offside-js--init using-mouse">

    The class we’re looking for here is provider

    #2503778
    David
    Staff
    Customer Support

    Is it possible to see the archive ?

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