WordPress 內建函式 get_posts() 用於呼叫指定的一篇或多篇文章,函式提供多個引數,可以設定呼叫文章的指定分類、自定義欄位、文章型別、文章狀態、顯示順序等條件,文章以陣列的形式返回,透過迴圈語句輸出即可。

程式碼結構:

<span%20style="color: #000000; font-weight: bold;"><?php get_posts<span%20style="color: #009900;">(<span%20style="color: #000088;">$args<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>

返回陣列型的變數,$args 是該函式必要的變數

變數 $args 引數:

<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #000088;">$args <span%20style="color: #339933;">= <span%20style="color: #990000;">array<span%20style="color: #009900;">( <span%20style="color: #666666; font-style: italic;">//顯示的文章數量 <span%20style="color: #0000ff;">'numberposts' <span%20style="color: #339933;">=> <span%20style="color: #cc66cc;">10<span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//以第幾篇文章為起始位置 <span%20style="color: #0000ff;">'offset' <span%20style="color: #339933;">=> <span%20style="color: #cc66cc;">0<span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//呼叫分類的 ID,多個用逗號將分類編號隔開,或傳遞編號陣列,可指定多個分類編號。 <span%20style="color: #0000ff;">'category' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//文章排序規則 www.weixiaoduo.com <span%20style="color: #0000ff;">'orderby' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'post_date'<span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//升序、降序 'ASC' —— 升序 (低到高)'DESC' —— 降序 (高到底)<span%20style="color: #0000ff;">'order' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'DESC'<span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//要顯示文章的 ID <span%20style="color: #0000ff;">'include' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//要排除文章的 ID <span%20style="color: #0000ff;">'exclude' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//自定義欄位名稱 <span%20style="color: #0000ff;">'meta_key' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//自定義欄位的值,配合上一個引數,來選擇顯示符合自定義欄位數值的文章。 <span%20style="color: #0000ff;">'meta_value' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//post(日誌)——預設,page(頁面),attachment(附件),any ——(所有)<span%20style="color: #0000ff;">'post_type' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'post'<span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//文章的 mime 型別 <span%20style="color: #0000ff;">'post_mime_type' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//要顯示文章的父級 ID <span%20style="color: #0000ff;">'post_parent' <span%20style="color: #339933;">=> <span%20style="color: #339933;">,   <span%20style="color: #666666; font-style: italic;">//文章狀態 <span%20style="color: #0000ff;">'post_status' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'publish' <span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>

orderby 排列順序可選的值:

  • 『author』 —— 按作者數值編號排序
  • 『category』 —— 按類別數值編號排序
  • 『content』 —— 按內容排序
  • 『date』 —— 按建立日期排序
  • 『ID』 —— 按文章編號排序
  • 『menu_order』 —— 按選單順序排序。僅頁面可用。
  • 『mime_type』 —— 按 MIME 型別排序。僅附件可用。
  • 『modified』 —— 按最後修改時間排序。
  • 『name』 —— 按存根排序。
  • 『parent』 —— 按父級 ID 排序
  • 『password』 —— 按密碼排序
  • 『rand』 —— 任意排序結果
  • 『status』 —— 按狀態排序
  • 『title』 —— 按標題排序
  • 『type』 —— 按型別排序

示例:呼叫指定 ID 為 1,3,4,6 的四篇文章

<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #000088;">$posts <span%20style="color: #339933;">= get_posts<span%20style="color: #009900;">(<span%20style="color: #0000ff;">"numberposts=4&post_type=any&include=1,3,4,6"<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #b1b100;">if<span%20style="color: #009900;">(<span%20style="color: #000088;">$posts<span%20style="color: #009900;">) <span%20style="color: #339933;">: <span%20style="color: #b1b100;">foreach<span%20style="color: #009900;">( <span%20style="color: #000088;">$posts <span%20style="color: #b1b100;">as <span%20style="color: #000088;">$post <span%20style="color: #009900;">) <span%20style="color: #339933;">: setup_postdata<span%20style="color: #009900;">( <span%20style="color: #000088;">$post <span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?> <li> <h2><a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>" target="_blank"><span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?></a></h2> <div > <a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">if<span%20style="color: #009900;">(<span%20style="color: #009900;">(<span%20style="color: #990000;">function_exists<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'has_post_thumbnail'<span%20style="color: #009900;">)<span%20style="color: #009900;">) <span%20style="color: #339933;">&& <span%20style="color: #009900;">(has_post_thumbnail<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #009900;">{<span%20style="color: #000088;">$thumbnail_src <span%20style="color: #339933;">= wp_get_attachment_image_src<span%20style="color: #009900;">( get_post_thumbnail_id<span%20style="color: #009900;">(get_the_ID<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #009900;">) <span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?><img src="<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo <span%20style="color: #000088;">$thumbnail_src<span%20style="color: #009900;">[<span%20style="color: #cc66cc;">0<span%20style="color: #009900;">]<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>"/><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #009900;">}<span%20style="color: #b1b100;">else <span%20style="color: #009900;">{<span%20style="color: #000000; font-weight: bold;">?><img alt="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" src="<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo catch_that_image<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"/><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #009900;">} <span%20style="color: #000000; font-weight: bold;">?></a> </div> <div > <p><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo <span%20style="color: #990000;">mb_strimwidth<span%20style="color: #009900;">(<span%20style="color: #990000;">strip_tags<span%20style="color: #009900;">(apply_filters<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'the_content'<span%20style="color: #339933;">, <span%20style="color: #000088;">$post<span%20style="color: #339933;">-><span%20style="color: #004000;">post_content<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #339933;">, <span%20style="color: #cc66cc;">0<span%20style="color: #339933;">, <span%20style="color: #cc66cc;">100<span%20style="color: #339933;">,<span%20style="color: #0000ff;">"……"<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?></p> </div> <div > <a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"> 閱讀全文</a> </div> </li> <span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">endforeach<span%20style="color: #339933;">; <span%20style="color: #b1b100;">endif<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>