- This topic has 10 replies, 3 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
November 18, 2022 at 2:29 pm #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!November 19, 2022 at 3:30 am #2422648David
StaffCustomer SupportHi there,
my approach would be to:
1. Save a global color for your eg.
--provider-accentin 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 ?
November 19, 2022 at 10:03 am #2423193John
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
– AppAnd 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.
November 19, 2022 at 10:21 am #2423208John
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.
November 20, 2022 at 5:35 am #2423917David
StaffCustomer SupportWhen 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 theis_singularis correct and theyour_custom_tax_slugmatches 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.
providerorapp2. 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.
November 21, 2022 at 12:46 pm #2426157John
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-accentnot--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); }November 22, 2022 at 5:50 am #2427087David
StaffCustomer SupportGlad to be of help. And for my own sanity and OCD i corrected the CSS above 🙂
January 20, 2023 at 2:55 pm #2503469John
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?
January 20, 2023 at 5:28 pm #2503529Ying
StaffCustomer SupportI 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.
January 20, 2023 at 11:15 pm #2503616John
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
providerJanuary 21, 2023 at 4:00 am #2503778David
StaffCustomer SupportIs it possible to see the archive ?
-
AuthorPosts
- You must be logged in to reply to this topic.