Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the post Author ID outside the WP Loop

What I'm trying to accomplish is the code will auto detect the Author ID <?php the_author_ID(); ?> of the blogpost. Once it has the author ID, it will do a simple task. If the author # is equal to 2, call function sBadong. Else, if author ID is equal to 3, call function sJade. if two condition was not met, call function sBen.

here's my code, but it's not working. I dunno what wrong's with it. can you help?

<?php
$author_id=$post->post_author;
if ($author_id == "2") {
    echo sBadong();
} elseif ($author_id == "3") {
    echo sJade();
} else {
    echo sBen();
}
?>

The problem with the code above is it's not reading the Author Number of the post. It always returning the function sBen(); and ignore all if and else statements.

like image 689
Kareen Lagasca Avatar asked May 09 '14 07:05

Kareen Lagasca


People also ask

How do I find the author ID of a WordPress post?

php $author_id=$post->post_author; ?> it will give you current author id.

How do I get the post author image in WordPress?

The easiest way to add an author photo in WordPress is by using the PublishPress Authors plugin. This plugin lets authors edit their own profile and author photo by adding a new 'Author Profile' area to their WordPress dashboard.

How do I find the current page id in WordPress?

get_the_ID() Retrieves the ID of the current item in the WordPress Loop.


1 Answers

Please use function as following:

$author_id = get_post_field ('post_author', $post_id);

It will work.

like image 131
alyosha Avatar answered Sep 21 '22 22:09

alyosha