Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active January 3, 2019 23:08
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 billerickson/ad862c6b0c9f235f999b6645a289fe82 to your computer and use it in GitHub Desktop.
Save billerickson/ad862c6b0c9f235f999b6645a289fe82 to your computer and use it in GitHub Desktop.
<?php
/**
* Display Posts Shortcode, add category classes
* @see https://displayposts.com/2019/01/03/add-category-classes/
*/
function be_dps_add_category_classes( $classes ) {
$categories = get_the_terms( get_the_ID(), 'category' );
if( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
foreach( $categories as $category ) {
$classes[] = 'cat-' . $category->slug;
}
}
return $classes;
}
add_filter( 'display_posts_shortcode_post_class', 'be_dps_add_category_classes' );
@critterles
Copy link

Thanks for the quick response. While I coded many years ago, I'm still working on CSS. Can the above code be added to the Custom CSS area at the bottom of the CSStyle file? or maybe using the WordPress Customization option for CSS? In your example is "catagory" and "Cat" variables for the category I want to affect? Also where would I set the font size and color in your statement?

I found this code but it doesn't seem to work.
.category-news {
font-size: 18px;
font-style: italic;
}
Thanks Again

@FredCohn
Copy link

FredCohn commented Jan 22, 2018

there is a typo in the code snippet @ line 11: "$categoriess" should be "$categories"

@billerickson
Copy link
Author

This code snippet goes in a core functionality plugin or Code Snippets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment