Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oliviercreativ/38d2248f49f054c46e6a45021fb91b7c to your computer and use it in GitHub Desktop.
Save Oliviercreativ/38d2248f49f054c46e6a45021fb91b7c to your computer and use it in GitHub Desktop.
Wordpress with ACF - add background color to category / categories
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