In the tutorials requests Trello board, a user asked:
I'm styling posts differently based on what category they are in, but I'd also like to style them differently based on which author they are written by. I am adding the category name to the body class via the code I'll put in the comments. I'd like to know how to add the author name to the body or post class, as well. Thank you!
This members-only tutorial provides the code to add post author's nicename to body classes on single posts and to post classes on non-singular listing pages in WordPress.
Sample screenshots:
A single post written by the user having a nicename of "sridhar":
A single post written by the user having a nicename of "usera":
Posts page:
Add the following in child theme's functions.php:
To view the full content, please sign up for the membership.
Already a member? Log in below or here.
This worked so well! But now it’s giving me an error:
“Notice: Undefined variable: post_id”
The line it references is this one:
$classes[] = 'author-' . get_the_author_meta( 'user_nicename', get_post_field( 'post_author', $post_id ) );
Sridhar, did you ever update this code so that there was no longer an error? Thank you!
Nevermind, I think I have it working!