0

I want to add CSS code to the header of all the posts by a particular author.

I tried the following solution:

function hide_author_box() {
if (is_author('ritesh')) {
    ?>
<style>
    .author-box {
        display: none;
    }
</style>
    <?php
    }
}

But it doesn't work. How do I fix it?

0

is_author() is not for determining current user, it checks whether the Author archive page is in display or not.

Your call would be to use wp_get_current_user() or get_current_user_id() (if you are comfortable with user ID) or any similar function WP have.

Example of using wp_get_current_user()

<?php
$current_user = wp_get_current_user();

if ( 'ritesh' === $current_user->user_login ) {
    // do something
}
0

You're not far off.

is_author() returns a boolean meaning if you are on the archive page for Ritesh it will return true, and your code will execute.

Based on your post, I think you need to look up the page author based on the current post object and check against that instead. So your total code might look like this:

$id = get_queried_object()->post_author;
$author = get_the_author_meta('nickname', $id);
if($author == 'Ritesh') {
    //... Do some code here
}
  1. $id: This gets the current post object of the blog/page/post you are on, and returns the post authors ID
  2. Next we fetch the nickname as set in the User panel based on the ID we fetched earlier
  3. Then we compare the nickname against the name you want, and execute code based on that.

This should hopefully get you started :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.