WordPress 中,一篇文章的資訊有很多,比如 ID 、釋出時間、文章型別等等,在從資料庫中查詢到文章之後,需要多次的獲取文章資訊,所以 WordPress 會把文章儲存在一些變數裡,那這麼多的文章資訊是透過什麼方式儲存到一個變數裡的呢?就是這個 WP_Post 物件。

WP_Post 物件提供了儲存文章資訊的變數,而且還提供了一些方便處理文章資訊的方法,每一篇文章 WordPress 都會 new 一個 WP_Post 物件,然後把資訊塞到裡邊儲存起來,下邊我們來瞭解一下這個類的屬性。

屬性

這裡的屬性指的也就是儲存的文章資訊:

名稱 型別 簡介
ID 整數 文章 ID
post_author 字串 文章作者的使用者 ID
post_name 字串 文章別名 (slug)
post_type 字串 文章型別
post_title 字串 文章標題
post_date 字串 文章釋出的本地時間 (格式:0000-00-00 00:00:00)
post_date_gmt 字串 文章釋出的 GMT 時間 (格式:0000-00-00 00:00:00)
post_content 字串 文章內容
post_excerpt 字串 文章摘要
post_status 字串 文章狀態
comment_status 字串 文章評論狀態 (open 或 closed)
ping_status 字串 文章 ping 狀態 (open 或 closed)
post_password 字串 文章密碼
post_parent 整數 文章父級 ID(預設是 0)
post_modified 字串 文章最後修改的本地時間 (格式:0000-00-00 00:00:00)
post_modified_gmt 字串 文章最後修改的 GMT 時間 (格式:0000-00-00 00:00:00)
comment_count 字串 評論數量 (字串形式的陣列)
menu_order 字串 頁面的排序順序

用法

獲取當前文章 ID:

$examplePost=get_post();

echo$examplePost->ID;

其它

此類位於:wp-includes/post.php