I found the correct syntax is:
if ( is_single() && in_category ( '5' ) ) {
However, I solved the problem using this code:
//add category classes to single.php
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array
return $classes;
}
(or you could try using a plugin such as: https://wordpress.org/plugins/ambrosite-body-class-enhanced/). From there, you can style with css.
Related Posts:
- How do I get the category URL from get_the_category?
- Create single.php for specific category by category id
- How to get category id in single.php wordpress?
- in_category() works in single.php but not in page.php?
- previous/next_post_link in the same sub-category?
- Send to different single pages by category when multiple categories exist
- How to make the link to the category for a post go to corresponding page number in archive?
- Single.php Category Post Count
- Problems with my conditionals in single.php by category
- Get next post in same category not working WP 4.7.4
- Simple way to style posts of a single category differently in the loop and in single
- How can I check which archive has referred the single? [closed]
- Displaying links to all posts of the same category on the post page
- Ignore the Ad on Specific Categories
- custom wp_list_categories in single.php
- loop in single.php of the same category
- Display Some text in some specific category not all category
- When category name and title are the same … WP will select archive.php instead of single.php
- Using template file for single post in sub-directory multisite
- Code to display category-specific single.php is overriding normal single.php, too
- Display Referring Category on single post?
- Modify / Rewrite single template url structure
- How do I get a single page navigation depending on the previous page?
- Can I set a default featured image for a category?
- Could not insert term into the database
- Weird behaviour when adding terms/term_taxonomies programmatically
- How can I get category ID by category name?
- Randomise results from a category page?
- Adding Custom User Profile data based upon Categories
- Only show category to certain user levels without plugin
- Custom Query to search through categories
- Order posts by category name
- How can I make wp_list_categories output li with category-slug as class, for its children?
- Get sticky post from category?
- Remove word “Category” from WooCommerce product page [closed]
- Output link to category from WP_Query loop of woocommerce products
- Display name of the last child category
- If in category to be inside of a function
- How to exclude posts from a category when using this particular format
- Viewing category pages without the word ‘category’ in URL
- Different excerpt styles per category, but chronological
- Show only one category
- Pagination Not Working on Category.php page
- How to view WordPress’ default category IDs?
- Filter Home Page Posts in Buddypress [closed]
- Broken category pagination
- Color different for the current category
- how to change template?
- Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Product Category WooCommerce
- Hard coded main navigation
- one category template for multiple categories
- Display posts separated by Category in Author’s page
- How can I define a custom archive for childs of a specific category?
- Category shows only top level child categories
- The arrangement of categories always change while posting article
- If in_category not working for multiple single.php pages
- Page display certain Category Posts
- REST API: Display Category names in JSON?
- List posts in alphabetical order
- Where is the category of a post stored?
- Categories list loop – add separator every 3 categories [closed]
- Add image to each podcast series (or category) in headless WordPress instance [closed]
- wp_list_pages() exclude category
- How to define multi categories posts number
- Get the ID of 10 Latest Pages stored in a WordPress Category
- Admin: Navigation Menus do not show empty categories/taxonomies in the list’s search tab
- how to separate categories in loop.php?
- How can i remove the posts category class names from body_class
- Different Category Headers using in_category and elseif
- related articles using shortcodes
- Insert HTML content in WP Query at specific point
- How can I ask to an existing category?
- Change categories count to include media attachments
- How to Sync Menu, Widgets and other masters from Main Website to its Sub Site
- Show category meta in loop on homepage
- Custom meta boxes ID prefix showing on first category but not others
- Exclude category from foreach loop
- How to store category and tags separatly on wordpress?
- Show Secondary Sidebar on Category pages (archive)
- Get categories of queried posts
- Get post content intro text on category.php?
- Conditional category query breaking?
- Unable to browser posts in categories
- 3.1 post formats and specific categories/custom taxonomies
- Duplicate new categories across multisite network
- Inline If statement to echo CSS [closed]
- Template category.php with page (no posts)
- How to create Child Category page from scratch at wordpress?
- How to set featured image as background for a specific category?
- Confused about category.php template – not working
- Limit Category/Tag Dropdown To blog_id in Multisite Install With Global Categories/Tags
- Adding relevant categories to related post
- How can I add a Categories page link to a menu? [closed]
- Media Library filter returns undefined function get_current_screen
- Way to show content of a post, but if exceeds character limit revert to excerpt?
- get IDs from links categories
- get posts from 2 categories. (2 posts)
- Display all subcategories for a product category in woocommerce
- Converting Categories (with ACF fields) to custom taxonomies
- Why does my WordPress index has a category?