Forked from adamrosloniec/gist:58467267db06603dca73ff222e4b2e50
Created
July 25, 2022 14:06
-
-
Save Oliviercreativ/38d2248f49f054c46e6a45021fb91b7c to your computer and use it in GitHub Desktop.
Wordpress with ACF - add background color to category / categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_blog_category_color(){ | |
$categories = get_categories(array( 'hide_empty' => 0, )); | |
if ( ! empty( $categories ) ) { | |
echo '<style type="text/css">'; | |
foreach( $categories as $category ) { | |
$category_taxonomy = $category->taxonomy; | |
$category_id = $category->term_id; | |
$category_slug = $category->slug; | |
$category_color = get_field('blog_category_color',$category_taxonomy . '_' . $category_id); | |
if (!empty($category_color)) { | |
echo '.blog-category-color--' . $category_slug . '{background-color:' . $category_color . '!important}'; | |
} | |
} | |
echo '</style>'; | |
} | |
} | |
add_action('wp_head','add_blog_category_color'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment