wordpress关于日志的常用函数-get_posts

模板标签-get_posts
说明
这是一个用于创建多环路的简单标签。用于检索最新的或者匹配条件的文章列表。
注意,虽然参数与get_pages方法类似,但是有几个参数略有不同。
 
用法
  <?php $posts_array = get_posts( $args ); ?>  
默认情况下的用法
<?php
 $args = array(
    'numberposts'     => 5,
    'offset'          => 0,
    'category'        => ,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'include'         => ,
    'exclude'         => ,
    'meta_key'        => ,
    'meta_value'      => ,
    'post_type'       => 'post',
    'post_mime_type'  => ,
    'post_parent'     => ,
    'post_status'     => 'publish' );
$posts_array = get_posts( $args );
 ?> 
 
最初到现在的文章列表
如果在博客首页上只设置显示一篇文章,但同时希望在分类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 ();  
      } 
}   
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值