2017-04-12 103 views
0

我使用下面的代碼在Wordpress中獲取帖子信息。我在網上找到了這個代碼,並添加了兩個變量「date」和「author」。但是,這些目前沒有按要求/預期工作。在Wordpress中獲取帖子作者和日期

對於「日期」,它將返回完整的日期/時間戳(即2017-03-08 15:12:39),而不是使用get_the_date()時檢索的格式良好的日期。 (即2017年3月8日)。我可以調整我的標記以格式化日期嗎?

對於「作者」,什麼都沒有被返回?

在此先感謝。

$postData = get_posts($postParameters); 
$posts = array(); 

foreach($postData as $post) { 
    $post = array(
     "id"  => $post->ID, 
     "title" => $post->post_title, 
     "excerpt" => $post->post_excerpt, 
     "date"  => $post->post_date, 
     "author" => $post->post_author, 
     "slug"  => $post->slug, 
     "image" => array("url" => get_the_post_thumbnail_url($post->ID)), 
     "link"  => get_permalink($post->ID), 
     "category" => get_the_category($post->ID)[0]->name 
    ); 

    array_push($posts, $post); 
} 

回答

1

這是因爲您正在檢索一個對象,在收到數據後,您需要按照需要的方式對其進行格式化。例如:

這將檢索與該發佈信息的對象:從對象

<?php $postData = get_posts($postParameters);?> 

現在,你收到的日期爲:

[post_date] => 2016-09-20 15:42:55 

所以表現出其作爲一個不錯的日期你需要像這樣格式化:

<?php echo '<p>'.get_the_date('F, j Y', $postData[0]->ID).'</p>'; ?> 

並且與作者一起,對象返回作者ID這是一個數字:

<?php echo '<p>'.the_author_meta('display_name', $postData[0]->post_author).'</p>'; ?> 

我希望它可以幫助你:用下面的代碼就可以顯示出作者的顯示名稱

[post_author] => 1 

現在! 享受!

+0

這很好,非常感謝您的幫助! –

+0

歡迎我的開發者朋友! – ScyGeek

相關問題