Forums

[resolved] How To Get a Post Author's ID (In Single Post View)? (6 posts)

  1. Walker
    Member
    Posted 1 year ago #

    Hello.

    I want to be able to get the post author's name or ID in single post view, but I couldn't find a working function in the WordPress codex yet.

    get_the_author() can be used only inside The Loop and I want to get the information I need inside the <head> part of my site (before the Loop starts).

  2. Edward Caissie
    Member
    Posted 1 year ago #

    This seems to be more than meets the eye ... once you have the "post author's name or ID" what will you be doing with it?

    For example, if this is style related, the body_class() function provides a class unique for each author.

  3. MichaelH
    Volunteer
    Posted 1 year ago #

    I believe posts[0]->post_author would have the single post author ID

  4. Walker
    Member
    Posted 1 year ago #

    I will display custom JavaScript code based on the author.

    I will try the solution that MichaelH provided and post. If I don't post, it means it works and I have forgot about this thread :D

  5. Walker
    Member
    Posted 1 year ago #

    It does work! Only instead of posts[0] it should be $posts[0].

    This is the code that works for me:

    $getid = $posts[0]->post_author;
    if($getid == 5){
    // Do something that is specific for user with ID 5
    }
    if($getid == 2){
    // Do something for user with ID = 2
    }
    // And so on...
  6. MichaelH
    Volunteer
    Posted 1 year ago #

    Good catch. Marking this resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic