一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress中 get_posts() 函数用法详解

时间:2016-07-19 编辑:简简单单 来源:一聚教程网

wordpress 的内置函数 get_posts 用来提取数据库中多篇指定文章或者是随机文章,今天研究了下,其使用的方法还不算是很麻烦,主要是解析其函数都有哪些方法,先看一下用法,代码如下:

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

下面再看一下$args的参数:


< ?php
$args = array(
//需要提取的文章数
"numberposts"     => 10,//以第几篇文章为起始位置
"offset"          => 0,//分类的ID,多个用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。
//大部分 CMS 使用该函数的重点。
"category"        => ,//排序规则(注1)
"orderby"         => "post_date",//升序、降序 "ASC" —— 升序 (低到高)  "DESC" —— 降序 (高到底)
"order"           => "DESC",//要显示文章的ID
"include"         => ,//要排除文章的ID
"exclude"         => ,//自定义字段名称
"meta_key"        => ,
//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。
"meta_value"      => ,//post(日志)——默认,page(页面),
//attachment(附件),any —— (所有)
"post_type"       => "post",//文章的 mime 类型
"post_mime_type"  => ,//要显示文章的父级 ID
"post_parent"     => ,//文章状态
"post_status"     => "publish" );
?>

现在我们再来看它的用法:

最初到现在的文章列表

如果在博客首页上只设置显示一篇文章,但同时希望在分类ID 1中显示最近五篇文章的链接,可使用如下代码:

 

      
    < ?php  
    global $post;  
    $myposts = get_posts('numberposts=5&offset=1&category=1');  
    foreach($myposts as $post) :  
    ?>     
      
  • < ?php the_title(); ?>
  •   
     < ?php endforeach; ?>  
     

注意:使用offset时,以上查询仅适用于含有一篇以上文章的分类,否则无法输出。

获取所有文章资料

默认情况下get_posts无法获取一些文章相关数据,如通过 the_content()获取文章内容或序列ID。调用内部函数setup_postdata(),以$post 数组为其自变量,可以解决这一问题:

< ?php  
$lastposts = get_posts('numberposts=3');  
foreach($lastposts as $post) :     
   setup_postdata($post);  
?>  


< ?php the_title(); ?>

  
< ?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); 
?>   

 
< ?php the_date(); ?>  


< ?php the_title(); ?>     
< ?php the_excerpt(); ?>  
  
< ?php endforeach; ?>

注意:排序参数在2.6版本中有所修改。此代码适用于新排序格式。详细内容参见参数。

任意文章

用MySQL RAND()函数指定排序参数的值,可以显示出随意选择的五篇文章:

  • A random selection of my writing

         
      
        
      < ?php  
      $rand_posts = get_posts('numberposts=5&orderby=rand');  
      foreach( $rand_posts as $post ) :  
      ?>     
        
    • < ?php the_title(); ?>
    •   
      < ?php endforeach; ?>     
        
      

显示所有附件

不用模板中任何循环进行本项操作。

(使用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);   
      } 
}   
?>

热门栏目