WordPress的Get_Posts()函数详解

技术流评论9,7362阅读模式
WP中获取POST有两个主要函数,Get_post()和Get_Posts()。一个是获取单文章,另外一个是获取多文章,其中,官网对Get_posts()函数的描述很简单。但有的时候描述越简单的函数,使用起来却并没有那么简单。下面我通过一个案例来解释一下WP_Posts()函数。
下面这个案例,是获取页面上那篇文章的分类,然后显示这个分类下的5篇文章。显示最新的5篇。
代码之中Categories是数组,获取分类信息的。然后得到这个数组中的文章。随后开始使用Get_Posts()方法。获取到文章的篇数和确定该文章属于特定的分类。
<?php/*single page?show current category articles*/?>
<?phpif ( is_single() ) :global $post;
$categories = get_the_category();
foreach ($categories as $category) :    ?>    
<li class="widget widget_recent_entries" id="<?php $category->term_id;?>-posts">        
<h2 class="widgettitle"><?php echo $category->name; ?></h2>        
<ul>        
<?php        $posts = get_posts('numberposts=5&category='. $category->term_id);    
    foreach($posts as $post) :        ?>            
<li>            
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>            </li>       
 <?php endforeach; ?>        </ul>    </li>
<?phpendforeach; endif ; ?>
<?php/*end show current category articles*/?>

其中,numberposts=5&category='. $category->term_id,是在获取本分类下的5篇文章。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

基于这段代码,结合Get_Posts()函数,我们可以对这种获取进行更细化的操作。比如获取随机的文章,或获取最早、最晚的文章。都是可以的。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

首先看一下Get_Posts()函数的官方说明。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

get_posts 函数详解

该函数属于 WordPress 的内置函数,用于在 WordPress 中提取多篇指定或随机文章。
使用方法:文章源自原紫番博客-https://www.yuanzifan.com/53255.html

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

$args是该函数必要的变量,也就是该函数的参数。
  get_posts( $args )将返回数组型的变量。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

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

变量参数详解

上面介绍了默认的数组中的类型,其中比较重要的是排序,即Orderby。Wordperss官方给出的参数是以下这些:
‘author’ —— 按作者数值编号排序
‘category’ —— 按类别数值编号排序
‘content’ —— 按内容排序
‘date’ —— 按创建日期排序
‘ID’ —— 按文章编号排序
‘menu_order’ —— 按菜单顺序排序。仅页面可用。
‘mime_type’ —— 按MIME类型排序。仅附件可用。
‘modified’ —— 按最后修改时间排序。
‘name’ —— 按存根排序。
‘parent’ —— 按父级ID排序
‘password’ —— 按密码排序
‘rand’ —— 任意排序结果
‘status’ —— 按状态排序
‘title’ —— 按标题排序
‘type’ —— 按类型排序文章源自原紫番博客-https://www.yuanzifan.com/53255.html

实例刚我们讲到用数组去传参,当然我们也可以用字符串来给该函数传参,下面给一个简单的例子。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

需要注意的是,如果单纯想要用升序或者降序,只使用Order函数即可。Orderby这个函数的值不可以等于desc或者asc,那样会报错。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

下面几个简单的例子,帮大家理解Get_Posts()函数。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

//显示随机的3篇文章
<?php
$posts_rand = get_posts('numberposts=3&orderby=rand');
?>

//时间顺序从早到晚显示10篇文章
<?php
$posts_ten = get_posts('numberposts=10&order=asc');
?>
//显示10篇文章,但是排除分类序号为12的文章
<?php
 $posts_excupost = get_posts('numberposts=10&order=asc&exclude=12'); 
?>

希望以上几个实例,可以帮你更好的了解Wordpress的Get_Posts()方法。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

原梓番博客原创,转载注明出处。

需要Wordpress技术支持的可以点这里:WordPress主题插件修改配置

 

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证