久青草国产观看在线视频,在线观看欧美日女,777毛片,亚洲国产精品99久久久久久久

wordpress根據(jù)條件獲取文章列表函數(shù):get_posts()

2012-12-23 wordpress函數(shù)
  • 文章介紹
  • 快速入門
  • 評(píng)價(jià)&建議

說明:

這是一個(gè)用于創(chuàng)建多環(huán)路的簡(jiǎn)單標(biāo)簽。用于檢索最新的或者匹配條件的文章列表。注意,雖然參數(shù)與get_pages方法類似,但是有幾個(gè)參數(shù)略有不同。

用法:

  1. <?php?$posts_array?=?get_posts(?$args?);??>

默認(rèn)情況下的用法:

  1. <?php
  2. ?$args?=?array(
  3. ????'numberposts'?????=>?5,
  4. ????'offset'??????????=>?0,
  5. ????'category'????????=>?,
  6. ????'orderby'?????????=>?'post_date',
  7. ????'order'???????????=>?'DESC',
  8. ????'include'?????????=>?,
  9. ????'exclude'?????????=>?,
  10. ????'meta_key'????????=>?,
  11. ????'meta_value'??????=>?,
  12. ????'post_type'???????=>?'post',
  13. ????'post_mime_type'??=>?,
  14. ????'post_parent'?????=>?,
  15. ????'post_status'?????=>?'publish'?);
  16. $posts_array?=?get_posts(?$args?);
  17. ??>

示例應(yīng)用:

獲取最初到現(xiàn)在的文章列表

如果在博客首頁(yè)上只設(shè)置顯示一篇文章,但同時(shí)希望在分類ID 1中顯示最近五篇文章的鏈接,可使用如下代碼:

  1. <ul>
  2. <?php
  3. global?$post;
  4. $myposts?=?get_posts('numberposts=5&offset=1&category=1');
  5. foreach($myposts?as?$post)?:
  6. ?>
  7. ???<li><a?href="<?php?the_permalink();??>"><?php?the_title();??></a>
  8. </li>
  9. ?<?php?endforeach;??>
  10. ?</ul>

注意:使用offset時(shí),以上查詢僅適用于含有一篇以上文章的分類,否則無(wú)法輸出。

獲取所有文章資料:

默認(rèn)情況下get_posts無(wú)法獲取一些文章相關(guān)數(shù)據(jù),如通過 the_content()獲取文章內(nèi)容或序列ID。調(diào)用內(nèi)部函數(shù)setup_postdata(),以$post 數(shù)組為其自變量,可以解決這一問題:

  1. <?php
  2. $lastposts?=?get_posts('numberposts=3');
  3. foreach($lastposts?as?$post)?:
  4. ???setup_postdata($post);
  5. ?>
  6. <h2><a?href="<?php?the_permalink();??>"?id="post-<?php?the_ID();??>"><?php?the_title();??></a></h2>
  7. <?php?the_content();??>
  8. <?php?endforeach;??>

不希望通過調(diào)用setup_postdata()來獲取文章的ID或內(nèi)容,或者獲取文章的任何相關(guān)數(shù)據(jù)時(shí)(數(shù)據(jù)存留在文章列表中),可以使用$post->COLUMN,COLUMN是文章數(shù)據(jù)表格的縱列名稱。因此$post->ID指明文章ID,$post->post_content指明文章內(nèi)容,以此類推。如要在頁(yè)面上顯示這些數(shù)據(jù),請(qǐng)使用PHP echo命令,如下所示:

  1. <?php?echo?$post->ID;??>

按標(biāo)題為最新發(fā)表文章排序:

以下代碼可按字母升序顯示最近發(fā)表的十篇文章的發(fā)布日期、標(biāo)題和摘要:

  1. <?php
  2. $postslist?=?get_posts('numberposts=10&order=ASC&orderby=title');
  3. foreach?($postslist?as?$post)?:
  4. ???setup_postdata($post);
  5. ?>
  6. <div>
  7. <?php?the_date();??>
  8. <br?/>
  9. <?php?the_title();??>
  10. <?php?the_excerpt();??>
  11. </div>
  12. <?php?endforeach;??>

注意:排序參數(shù)在2.6版本中有所修改。此代碼適用于新排序格式。詳細(xì)內(nèi)容參見參數(shù)。

任意文章

用MySQL RAND()函數(shù)指定排序參數(shù)的值,可以顯示出隨意選擇的五篇文章:

  1. <ul><li><h2>A?random?selection?of?my?writing</h2>
  2. ???<ul>
  3. <?php
  4. $rand_posts?=?get_posts('numberposts=5&orderby=rand');
  5. foreach(?$rand_posts?as?$post?)?:
  6. ?>
  7. ???<li><a?href="<?php?the_permalink();??>"><?php?the_title();??></a></li>
  8. <?php?endforeach;??>
  9. ???</ul>
  10. </li></ul>

顯示所有附件

不用模板中任何循環(huán)進(jìn)行本項(xiàng)操作。(使用2.5版本后的get_children()函數(shù)相對(duì)方便。)

  1. <?php
  2. $args?=?array(
  3. ???????'post_type'?=>?'attachment',
  4. ???????'numberposts'?=>?-1,
  5. ???????'post_status'?=>?null,
  6. ???????'post_parent'?=>?null,?//?any?parent?
  7. ???????);
  8. $attachments?=?get_posts($args);
  9. if?($attachments)?{
  10. ??????foreach?($attachments?as?$post)?{
  11. ??????????????setup_postdata($post);
  12. ??????????????the_title();
  13. ??????????????the_attachment_link($post->ID,?false);
  14. ??????????????the_excerpt();
  15. ??????}
  16. }
  17. ?>

顯示最新文章的附件

在The_Loop($post->ID可用)中進(jìn)行本類操作。

  1. <?php
  2. $args?=?array(
  3. ???????'post_type'?=>?'attachment',
  4. ???????'numberposts'?=>?-1,
  5. ???????'post_status'?=>?null,
  6. ???????'post_parent'?=>?$post->ID
  7. ???????);
  8. $attachments?=?get_posts($args);
  9. if?($attachments)?{
  10. ??????foreach?($attachments?as?$attachment)?{
  11. ??????????????echo?apply_filters('the_title',?$attachment->post_title);
  12. ??????????????the_attachment_link($attachment->ID,?false);
  13. ??????}
  14. }
  15. ?>

【參數(shù):WordPress 2.6+】
除“WordPress 2.5及更早版本”中列出的參數(shù)外,get_posts( )也能運(yùn)行query_posts( )所操作的參數(shù),目前這兩個(gè)函數(shù)在內(nèi)部使用相同的數(shù)據(jù)庫(kù)查詢代碼。
注意:2.6版本對(duì)一些排序選項(xiàng)做了更改。表格字段前不再含有post_字樣。如post_title已被改為title,post_data改為data。

參數(shù):WordPress 2.5及更早版本
$numberposts
(整數(shù))(可選)將要返回的文章數(shù)量。將其設(shè)為0可在每頁(yè)上顯示最大數(shù)量文章數(shù),設(shè)為-1可消除限制。
默認(rèn)值:5
$offset
(整數(shù))(可選)以最新文章為起始位
默認(rèn)值:0
$category
(整數(shù))(可選)僅顯示本分類編號(hào)下的文章。將分類編號(hào)設(shè)為負(fù)數(shù)(如果是3,設(shè)為-3),顯示結(jié)果不匹配。用逗號(hào)將分類編號(hào)隔開,或傳遞編號(hào)數(shù)組,可指定多個(gè)分類編號(hào)。
默認(rèn)值:None
$category_name
(字符)(可選)僅顯示本分類名稱或分類縮略名下的文章。
默認(rèn)值:None
$tag
(字符)(可選)僅顯示本標(biāo)簽縮略名下的文章。若指定多個(gè)用逗號(hào)隔開的標(biāo)簽縮略名,則返回結(jié)果是:所有文章都與某個(gè)標(biāo)簽匹配。若指定多個(gè)用空格隔開的標(biāo)簽縮略名,返回結(jié)果是:所有文章都與指定標(biāo)簽縮略名匹配。
默認(rèn)值:None
$orderby
(字符)(可選)按不同值(用空格隔開)為文章排序,包括:

‘a(chǎn)uthor’ —— 按作者數(shù)值編號(hào)排序
‘category’ —— 按類別數(shù)值編號(hào)排序
‘content’ —— 按內(nèi)容排序
‘date’ —— 按創(chuàng)建日期排序
‘ID’ —— 按文章編號(hào)排序
‘menu_order’ —— 按菜單順序排序。僅頁(yè)面可用。
‘mime_type’ —— 按MIME類型排序。僅附件可用。
‘modified’ —— 按最后修改時(shí)間排序。
‘name’ —— 按存根排序。
‘parent’ —— 按父級(jí)ID排序
‘password’ —— 按密碼排序
‘rand’ —— 任意排序結(jié)果
‘status’ —— 按狀態(tài)排序
‘title’ —— 按標(biāo)題排序
‘type’ —— 按類型排序
注意:

按編號(hào)排序和任意排序自2.5版本起啟用。
默認(rèn)值:post_date
$order
(字符)(可選)如何對(duì)$order排序。可能的值為:

‘ASC’ —— 升序 (低到高)
‘DESC’ —— 降序 (高到底)
默認(rèn)值:DESC
$include
(字符)(可選)希望顯示的文章編號(hào),用逗號(hào)和/或空格隔開。顯示六篇文章時(shí),下列值可能生效:

’45,63,78 94,128,140′
注意:該參數(shù)將改寫numberposts,offset,category,exclude,meta_key,meta_value,及post_parent參數(shù)。
默認(rèn)值:None
$exclude
(字符)(可選)不希望顯示的文章編號(hào),用逗號(hào)和/或空格隔開(參見$include參數(shù))。
默認(rèn)值:None
$meta_key 和$meta_value
(字符)(可選)僅顯示含有該關(guān)鍵詞和值的元(自定義)字段的文章。兩項(xiàng)參數(shù)都應(yīng)定義,否則無(wú)法運(yùn)行。
默認(rèn)值:None
$post_type
(字符)(可選)希望顯示的文章類型。可選項(xiàng)有:

post —— 默認(rèn)
page
attachment
any —— 任意文章類型
默認(rèn)值:post
$post-status
(字符)(可選)顯示特定狀態(tài)的文章。可選項(xiàng)有:

publish
private
draft
future
inherit —— 若$post_type設(shè)為附件,則此項(xiàng)為默認(rèn)選項(xiàng)
(blank)—— 所有狀態(tài)
默認(rèn)值:publish
$post_parent
(整數(shù))(可選)顯示此文章編號(hào)下的子文章
默認(rèn)值:None
$nopaging
(布爾型)(可選)激活或禁用分頁(yè)功能。如果禁用,$numberposts選項(xiàng)被略過。
默認(rèn)值:None

0 0

企業(yè)建站推薦正版商業(yè)主題,國(guó)內(nèi)專業(yè)團(tuán)隊(duì)開發(fā),完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計(jì)幫助1300+客戶成功建站,為站長(zhǎng)提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務(wù)熱線

wordpress建站咨詢