Skip to content

Instantly share code, notes, and snippets.

@codersantosh
Created May 5, 2020 06:06
Show Gist options
  • Select an option

  • Save codersantosh/70342f28e7c6114d62a4c2aa4506fe9d to your computer and use it in GitHub Desktop.

Select an option

Save codersantosh/70342f28e7c6114d62a4c2aa4506fe9d to your computer and use it in GitHub Desktop.
<?php
/**
* Add Category Dynamic Css
*
* Edit the selector on the function
* Change Selector as your theme/plugin
*/
if ( ! function_exists( 'prefix_get_post_categories_color' ) ) {
function prefix_get_post_categories_color( $imp = false) {
$important = $imp?' !important;':';';
/* device type */
$local_dynamic_css = '';
/*category color options*/
$args = array(
'orderby' => 'id',
'hide_empty' => 0
);
$categories = get_categories($args);
if( $categories ){
foreach ($categories as $category_list) {
$cat_color_css = '';
/*get customize id*/
$cat_color = 'gutentor-cat-' . esc_attr($category_list->term_id);
/* get category Color options */
$cat_color = get_option($cat_color);
$cat_color = json_decode($cat_color, true);
/* cat text color */
$cat_text_color = (!is_null($cat_color) && isset($cat_color['text-color']) && !empty($cat_color['text-color'])) ? $cat_color['text-color'] : '#1974d2';
if ($cat_text_color) {
$cat_color_css .= 'color:' . $cat_text_color . $important;
}
/* cat bg color */
$cat_bg_color = (!is_null($cat_color) && isset($cat_color['background-color']) && !empty($cat_color['background-color'])) ? $cat_color['background-color'] : '#ffffff';
if ($cat_bg_color) {
$cat_color_css .= 'background:' . $cat_bg_color . $important;
}
/* add cat color css */
if (!empty($cat_color_css)) {
/*Change Selector as your theme/plugin
.gutentor-categories .gutentor-cat-{$category_list->slug}
*/
$local_dynamic_css .= ".gutentor-categories .gutentor-cat-{$category_list->slug}{
" . $cat_color_css . "
}";
}
/* cat hover text color */
$cat_color_hover_css = '';
$cat_text_hover_color = (!is_null($cat_color) && isset($cat_color['text-hover-color']) && !empty($cat_color['text-hover-color'])) ? $cat_color['text-hover-color'] : '#ffffff';
if ($cat_text_hover_color) {
$cat_color_hover_css .= 'color:' . $cat_text_hover_color . $important;
}
/* cat hover bg color */
$cat_bg_hover_color = (!is_null($cat_color) && isset($cat_color['background-hover-color']) && !empty($cat_color['background-hover-color'])) ? $cat_color['background-hover-color'] : '#1974d2';
if ($cat_bg_hover_color) {
$cat_color_hover_css .= 'background:' . $cat_bg_hover_color . $important;
}
/*add hover css*/
if (!empty($cat_color_hover_css)) {
/*Change Selector as your theme/plugin
.gutentor-categories .gutentor-cat-{$category_list->slug}
*/
$local_dynamic_css .= ".gutentor-categories .gutentor-cat-{$category_list->slug}:hover{
" . $cat_color_hover_css . "
}";
}
}
}
return $local_dynamic_css;
}
}
/* get category color, when you need it*/
$categories_color_css = prefix_get_post_categories_color();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment