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
- Allow User to Edit Page Based on their Email
- Regex problem in an add_rewrite_rule
- Sort Posts Best Practice
- Get all children of custom post type using get_pages
- Admin Column Text Positioning
- Custom Taxonomy Creates Unwanted Integer Terms on Post Save
- How to quickly reorder posts in the admin panel that will persist for the wp-api
- Podcast Guest List
- Custom post type and permalink issue
- create a subcatagory for a custom post type and out put url as posttype/subcategory/item
- How to remove Trash link from custom post type
- Loop through custom posts by taxonomy and display the titles in a list, it is repeating 8 times
- retrieve permalinks of posts inside custom post type by using post ID
- How to change base in tag URL for custom post type, to not use “Custom Structure” set in Permalinks setting?
- Can’t access private custom posttype single- or archives-page
- Modifying the default post from wordpress
- Exclude latest post from WP_Query taxonomy term loop
- Getting custom post type author and date published
- Custom permalink structure for remote content pages
- how can i show a google map in custom post type
- Adding an extra parameter [string] to my posts’ permalink?
- Query Custom Post Type based on text input
- Order post grid by custom (acf) field – still not working
- display post count in archive page that have relation with another taxonomy term
- Automatically add CPT UI categories to the menu
- Stored meta from attachment, video length?
- How can I find out what template is my custom post type using?
- How to create a discussion post when a custom post is created?
- Can’t pick up a field created with Advanced Custom Fields
- How to List CPTs Under One Tax Term
- Remove the “View” Link in Post edit Admin
- Remove custom post type in url?
- Is it possible to remove the word “date” from archives?
- Need some hints for my own WP theme development