Skip to content

Instantly share code, notes, and snippets.

@billerickson billerickson/functions.php Secret
Last active Jan 3, 2019

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link

commented Dec 4, 2017

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

This comment has been minimized.

Copy link

commented Jan 22, 2018

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

@billerickson

This comment has been minimized.

Copy link
Owner Author

commented Jan 3, 2019

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
You can’t perform that action at this time.