CSS styling based on post category, Custom Post Type archive page
-
I’ve created a custom post type called “Events.” On the archives-events.php template, I’m using CSS flexbox to display the events in a grid, where each grid item is a separate event.
Some of the events will have an “open to public” category added to them. If the category “open to public” is applied to the event, then the category is displayed, otherwise, no category is displayed.
Each event has a background color, however, I want to give events categorized as “open to public” a different background color and a border. How can I do that on the CPT archive page?
Here is the code snippet from my archives-events.php page that I used to achieve to display the “open to public” category, if it exists:
<h5><?php $categories = get_the_category(); $separator = ' '; $output = ''; if($categories){ foreach($categories as $category) { if($category->name !== 'open to public') { $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator; } } echo trim($output, $separator); } ?>
- The topic ‘CSS styling based on post category, Custom Post Type archive page’ is closed to new replies.