在WP Loop外获取文章作者ID


Get the post Author ID outside the WP Loop

我想要完成的是代码将自动检测作者ID <?php the_author_ID(); ?>的博客文章。有了作者ID后,它将执行一个简单的任务。如果作者#等于2,调用函数sBadong。否则,如果作者ID等于3,调用函数sJade。如果两个条件都不满足,调用函数sBen。

这是我的代码,但它不工作。我不知道有什么问题。你能帮忙吗?

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

上面代码的问题是它没有读取文章的作者编号。它总是返回函数sBen();并且忽略所有if和else语句

请按以下方式使用函数:

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