The code isn’t working because you haven’t defined or retrieved the $current_user
or $post
variables from anyway. You’ve also got a !
here for some reason: !$current_user->ID
, which will just break the condition.
You need to use the appropriate functions to get their values, and also use is_single()
to make sure you’re viewing a single post (otherwise the post author could be missing or something unexpected).
add_filter(
'body_class',
function( $classes ) {
if ( is_single() ) {
$post = get_queried_object();
$user = wp_get_current_user();
if ( $user->ID == $post->post_author ) {
$classes[] = 'post-author';
}
}
return $classes;
}
);
Related Posts:
- Getting a list of custom posts by author
- Display Authors Comments on Profile Page
- the_author_posts_link() for authors of custom post types
- Adding %author% in custom post type URL structure?
- Author archive only for custom post type
- Custom loop for custom post type for current author
- Show Custom Post Type by Author
- Recent comments on author page?
- Get the terms of a custom taxonomy for a specific author in author template
- Prevent author role from editing all posts in custom post type?
- Display only custom post type count for current author on the “At a Glance” dashboard widget
- How to fetch custom post by Author?
- WP-Admin edit.php & post.php slowdown after import of 10k users
- Check if author or current user has posts published
- Get Author for Single Post in Custom Post Type
- How do I prefix blog post urls as mysite.com/blog/%postname%/ but allow authors to still be located at mysite.com/authors/%nicename%?
- How can I show a custom post type for users in the authors.php file?
- WordPress post count not showing for custom post type author
- How Can I Set the Post Author of a Post I Just Created With PHP?
- Custom Post Type and Labels
- Filter post by current 2 differents users id
- How to only show posts assigned to current user, only in certain post types
- get_attached_media() on author page not working
- Permit users with author role to edit a custom post type
- URL rewrite add author as base
- Edit the author of custom post type
- Can multiple authors be assigned on a single custom post?
- How to restrict author to only access one custom post type ?
- How to show featured image block in custom post type for Author?
- Author posts not appearing for custom post type
- Display an authors post on a single page only when they are logged in
- Select other roles as custom post authors
- Restrict category access to specific users/groups. Author always has access
- Find custom post type url by author
- Display latest posts of author in a custom post type
- Custom post type title of each author in his own post
- get post count for related author
- Display author box on just certain category posts?
- Display custom post counts for author , not default posts
- Use Author Ids or Names to echo different output inside loop
- Creating an option for writing a post without an author attribution for a group blog on WordPress
- Showing only posts from the current user who is logged in?
- “Author” custom post type
- how to display author name in custom post type
- Limit custom post type to the authors only on front-end!
- How do I get all authors posts of a custom post type outside loop
- Why is wp_list_authors not picking up Authors from CPT’s?
- Need to display author’s email id in the “Edit post” field in wp dashboard. How do I do this?
- Delete Post by User
- How to make id user can be view content
- Display posts on parent post if author coincides
- Author archive custom posts only?
- Set a static page as a user profile page?
- I want to link an author to everything from any post type that they publish on the website
- Custom Taxonomies in body class with parent slug
- Get author image for posts in loop on custom type posts page
- WordPress Author Posts Review After Every Change In The Same WordPress Post
- Custom Post Type Author Array Problem
- Show a custom field instead of username in the backend author area
- Custom Post Type supports author
- Author template – separate custom post type by custom taxonomy term for $curauth
- How is get_the_author connected to a custom post type?
- How to display Author Profile based on Custom field value
- how to show Author post type count
- Emailing Authors only when a CUSTOM POST TYPE post is published- not when edited later [duplicate]
- body class according to number of published custom posts
- Add body class to custom post type
- How to display custom field value on page?
- Displaying custom post types in front end
- Navigate posts with different post type that are in the same categories
- Custom taxonomy (categories) on custom post type return no results
- Page listing Custom Posts
- post_per_page ignored in WP_Query
- the_title not working
- Display different information of a custom post type
- Custom taxonomy wp_query woes.
- WordPress app page not found error for posts with hyphen in permalink
- Where is the sticky post option?
- get_the_tag_list() returns bad links
- Add term list as class
- Filter second dropdown (tax) based on first dropdown (cpt)
- Dump custom post type database
- uploading problem of wordpress theme
- Excluding a Custom Post Type with a specific tag using pre_get_posts
- Custom Post Type Link added to menu won’t show in frontend
- show all custom posts types in home page
- List of users that clicked a ‘Join’ button at single post
- Taxonomy archive 404ing (not term archive)
- Pagination doesn’t function properly for archive of a custom post type set as the front page
- Variable not working in WP_Query
- WP_query – Filter by tax_query and meta_query using multiple select
- How to group navigation items in the admin panel
- Hide meta box for everything BUT a certain custom post type
- Include php on a specific page template
- Create Array from custom post type to display a slider
- what custom post type this archive belongs to?
- Setting Post Title via Meta Data returns Auto-draft
- Tags not working in my custom post type
- Custom post-type metabox position
- wp_trash_post action hook with custom post type