模板标签-get_posts
说明
这是一个用于创建多环路的简单标签。
用法
<?php get_posts(‘arguments’); ?>
示例
最初到现在的文章列表
如果在博客首页上只设置显示一篇文章,但同时希望在分类ID 1中显示最近五篇文章的链接,可使用如下代码:
 <ul>   
<?php   
global $post;   
$myposts = get_posts('numberposts=5&offset=1&category=1');   
foreach($myposts as $post) :   
?>      
   <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>   
 <?php endforeach; ?>   
 </ul>
注意:使用offset时,以上查询仅适用于含有一篇以上文章的分类,否则无法输出。
获取所有文章资料
默认情况下get_posts无法获取一些文章相关数据,如通过 the_content()获取文章内容或序列ID。调用内部函数setup_postdata(),以$post 数组为其自变量,可以解决这一问题:
<?php   
$lastposts = get_posts('numberposts=3');   
foreach($lastposts as $post) :      
   setup_postdata($post);   
?>   
<h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>   
<?php the_content(); ?>   
<?php endforeach; ?>  
不希望通过调用setup_postdata()来获取文章的ID或内容,或者获取文章的任何相关数据时(数据存留在文章列表中),可以使用$post->COLUMN,COLUMN是文章数据表格的纵列名称。因此$post->ID指明文章ID,$post->post_content指明文章内容,以此类推。如要在页面上显示这些数据,请使用PHP echo命令,如下所示:
<?php echo $post->ID; ?>
按标题为最新发表文章排序
以下代码可按字母升序显示最近发表的十篇文章的发布日期、标题和摘要:
<?php  
$postslist = get_posts('numberposts=10&order=ASC&orderby=title');  
foreach ($postslist as $post) :       
   setup_postdata($post);  
?>    
<div>  
<?php the_date(); ?>   
<br /> 
<?php the_title(); ?>      
<?php the_excerpt(); ?>   
</div>   
<?php endforeach; ?>     
注意:排序参数在2.6版本中有所修改。此代码适用于新排序格式。详细内容参见参数。
任意文章
用MySQL RAND()函数指定排序参数的值,可以显示出随意选择的五篇文章:
<ul><li><h2>A random selection of my writing</h2>      
   <ul>   
<?php   
$rand_posts = get_posts('numberposts=5&orderby=rand');   
foreach( $rand_posts as $post ) :   
?>      
   <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>   
<?php endforeach; ?>      
   </ul>   
</li></ul>   
显示所有附件
不用模板中任何循环进行本项操作。
(使用2.5版本后的get_children()函数相对方便。)
<?php    
$args = array(  	
       'post_type' => 'attachment',  	
       'numberposts' => -1,  	
       'post_status' => null,  	
       'post_parent' => null, // any parent  	
       );   
$attachments = get_posts($args);  
if ($attachments) {  	
      foreach ($attachments as $post) {  		
              setup_postdata($post);  		
              the_title();  		
              the_attachment_link($post->ID, false);  		
              the_excerpt();  	
      }  
}    
?>  
显示最新文章的附件
在The_Loop ($post->ID可用)中进行本类操作。
<?php   
 
$args = array(  	
       'post_type' => 'attachment',  	
       'numberposts' => -1,  	
       'post_status' => null,  	
       'post_parent' => $post->ID  	
       );   
$attachments = get_posts($args);  
if ($attachments) {  	
      foreach ($attachments as $attachment) {  		
              echo apply_filters('the_title', $attachment->post_title);  		
              the_attachment_link($attachment->ID, false);  	
      }  
}    
?>  
参数:WordPress 2.6+
除“WordPress 2.5及更早版本”中列出的参数外,get_posts( )也能运行query_posts( )所操作的参数,目前这两个函数在内部使用相同的数据库查询代码。
注意:2.6版本对一些排序选项做了更改。表格字段前不再含有post_字样。如post_title已被改为title,post_data改为data。
参数:WordPress 2.5及更早版本
$numberposts
(整数)(可选)将要返回的文章数量。将其设为0可在每页上显示最大数量文章数,设为-1可消除限制。
默认值:5
$offset
(整数)(可选)以最新文章为起始位
默认值:0
$category
(整数)(可选)仅显示本分类编号下的文章。将分类编号设为负数(如果是3,设为-3),显示结果不匹配。用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。
默认值:None
$category_name
(字符)(可选)仅显示本分类名称或分类缩略名下的文章。
默认值:None
$tag
(字符)(可选)仅显示本标签缩略名下的文章。若指定多个用逗号隔开的标签缩略名,则返回结果是:所有文章都与某个标签匹配。若指定多个用空格隔开的标签缩略名,返回结果是:所有文章都与指定标签缩略名匹配。
默认值:None
$orderby
(字符)(可选)按不同值(用空格隔开)为文章排序,包括:
- ‘author’ —— 按作者数值编号排序
 - ‘category’ —— 按类别数值编号排序
 - ‘content’ —— 按内容排序
 - ‘date’ —— 按创建日期排序
 - ‘ID’ —— 按文章编号排序
 - ‘menu_order’ —— 按菜单顺序排序。仅页面可用。
 - ‘mime_type’ —— 按MIME类型排序。仅附件可用。
 - ‘modified’ —— 按最后修改时间排序。
 - ‘name’ —— 按存根排序。
 - ‘parent’ —— 按父级ID排序
 - ‘password’ —— 按密码排序
 - ‘rand’ —— 任意排序结果
 - ‘status’ —— 按状态排序
 - ‘title’ —— 按标题排序
 - ‘type’ —— 按类型排序
 
注意:
- 按编号排序和任意排序自2.5版本起启用。
 
默认值: post_date
$order
(字符)(可选)如何对$order排序。可能的值为:
- ‘ASC’ —— 升序 (低到高)
 - ‘DESC’ —— 降序 (高到底)
 
默认值:DESC
$include
(字符)(可选)希望显示的文章编号,用逗号和/或空格隔开。显示六篇文章时,下列值可能生效:
- ’45,63, 78 94,128, 140′
 
注意:该参数将改写numberposts,offset,category,exclude,meta_key,meta_value,及post_parent参数。
默认值:None
$exclude
(字符)(可选)不希望显示的文章编号,用逗号和/或空格隔开(参见$include参数)。
默认值:None
$meta_key 和 $meta_value
(字符)(可选)仅显示含有该关键词和值的元(自定义)字段的文章。两项参数都应定义,否则无法运行。
默认值:None
$post_type
(字符)(可选)希望显示的文章类型。可选项有:
- post —— 默认
 - page
 - attachment
 - any —— 任意文章类型
 
默认值:post
$post-status
(字符)(可选)显示特定状态的文章。可选项有:
- publish
 - private
 - draft
 - future
 - inherit —— 若$post_type设为附件,则此项为默认选项
 - (blank) —— 所有状态
 
默认值:publish
$post_parent
(整数)(可选)显示此文章编号下的子文章
默认值:None
$nopaging
(布尔型)(可选)激活或禁用分页功能。如果禁用,$numberposts选项被略过。
默认值:None
分类:中文手册