我正在使用此代碼來獲取已張貼在當前類別中的所有作者的名字:設定功能結果作爲變量
function categoryAuthors() {
$category_authors = array();
$args = array(
'posts_per_page' => -1,
'category' => $category,
'post_status' => 'publish'
);
$allposts = get_posts($args);
if ($allposts) {
foreach($allposts as $authorpost) {
$category_authors[$authorpost->post_author]+=1;
}
arsort($category_authors);
foreach($category_authors as $key => $author_post_count) {
$user = get_userdata($key);
$an_author = "'" . $user->first_name . "', ";
echo $an_author;
}
}
}
$theAuthors = categoryAuthors();
echo $theAuthors;
..這幾乎是工作,但是,如果我註釋掉回聲像這樣...
// echo $theAuthors;
...所有的電子郵件地址都在我的網頁上回應,無論。
我的理解是,一個函數不會被調用,直到你真的調用它,但它似乎被稱爲(輸出在頁面上),即使我不echo $ theAuthors。
無論如何,我想做一個變量,這是一個逗號分隔的作者名字列表,以便我可以在其他地方使用它。
我該如何解決這個問題?
乾杯。
你的函數需要_return_東西! – jszobody
什麼?您在使用'()'運算符時調用該函數。此外,你正在迴應函數體中的'$ an_author'變量。 –
你知道'echo'與'print'是一樣的嗎? –