WordPress 内置函数 get_posts() 用于调用指定的一篇或多篇文章,函数提供多个参数,可以设置调用文章的指定分类、自定义字段、文章类型、文章状态、显示顺序等条件,文章以数组的形式返回,通过循环语句输出即可。
代码结构:
<span%20style="color: #000000; font-weight: bold;"><?php get_posts<span%20style="color: #009900;">(<span%20style="color: #000088;">$args<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>
返回数组型的变量,$args 是该函数必要的变量
变量 $args 参数:
<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #000088;">$args <span%20style="color: #339933;">= <span%20style="color: #990000;">array<span%20style="color: #009900;">( <span%20style="color: #666666; font-style: italic;">//显示的文章数量 <span%20style="color: #0000ff;">'numberposts' <span%20style="color: #339933;">=> <span%20style="color: #cc66cc;">10<span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//以第几篇文章为起始位置 <span%20style="color: #0000ff;">'offset' <span%20style="color: #339933;">=> <span%20style="color: #cc66cc;">0<span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//调用分类的 ID,多个用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。 <span%20style="color: #0000ff;">'category' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//文章排序规则 www.weixiaoduo.com <span%20style="color: #0000ff;">'orderby' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'post_date'<span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//升序、降序 'ASC' —— 升序 (低到高)'DESC' —— 降序 (高到底)<span%20style="color: #0000ff;">'order' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'DESC'<span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//要显示文章的 ID <span%20style="color: #0000ff;">'include' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//要排除文章的 ID <span%20style="color: #0000ff;">'exclude' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//自定义字段名称 <span%20style="color: #0000ff;">'meta_key' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。 <span%20style="color: #0000ff;">'meta_value' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//post(日志)——默认,page(页面),attachment(附件),any ——(所有)<span%20style="color: #0000ff;">'post_type' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'post'<span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//文章的 mime 类型 <span%20style="color: #0000ff;">'post_mime_type' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//要显示文章的父级 ID <span%20style="color: #0000ff;">'post_parent' <span%20style="color: #339933;">=> <span%20style="color: #339933;">, <span%20style="color: #666666; font-style: italic;">//文章状态 <span%20style="color: #0000ff;">'post_status' <span%20style="color: #339933;">=> <span%20style="color: #0000ff;">'publish' <span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>
orderby 排列顺序可选的值:
- 『author』 —— 按作者数值编号排序
- 『category』 —— 按类别数值编号排序
- 『content』 —— 按内容排序
- 『date』 —— 按创建日期排序
- 『ID』 —— 按文章编号排序
- 『menu_order』 —— 按菜单顺序排序。仅页面可用。
- 『mime_type』 —— 按 MIME 类型排序。仅附件可用。
- 『modified』 —— 按最后修改时间排序。
- 『name』 —— 按存根排序。
- 『parent』 —— 按父级 ID 排序
- 『password』 —— 按密码排序
- 『rand』 —— 任意排序结果
- 『status』 —— 按状态排序
- 『title』 —— 按标题排序
- 『type』 —— 按类型排序
示例:调用指定 ID 为 1,3,4,6 的四篇文章
<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #000088;">$posts <span%20style="color: #339933;">= get_posts<span%20style="color: #009900;">(<span%20style="color: #0000ff;">"numberposts=4&post_type=any&include=1,3,4,6"<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #b1b100;">if<span%20style="color: #009900;">(<span%20style="color: #000088;">$posts<span%20style="color: #009900;">) <span%20style="color: #339933;">: <span%20style="color: #b1b100;">foreach<span%20style="color: #009900;">( <span%20style="color: #000088;">$posts <span%20style="color: #b1b100;">as <span%20style="color: #000088;">$post <span%20style="color: #009900;">) <span%20style="color: #339933;">: setup_postdata<span%20style="color: #009900;">( <span%20style="color: #000088;">$post <span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?> <li> <h2><a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>" target="_blank"><span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?></a></h2> <div > <a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">if<span%20style="color: #009900;">(<span%20style="color: #009900;">(<span%20style="color: #990000;">function_exists<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'has_post_thumbnail'<span%20style="color: #009900;">)<span%20style="color: #009900;">) <span%20style="color: #339933;">&& <span%20style="color: #009900;">(has_post_thumbnail<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #009900;">{<span%20style="color: #000088;">$thumbnail_src <span%20style="color: #339933;">= wp_get_attachment_image_src<span%20style="color: #009900;">( get_post_thumbnail_id<span%20style="color: #009900;">(get_the_ID<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #009900;">) <span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?><img src="<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo <span%20style="color: #000088;">$thumbnail_src<span%20style="color: #009900;">[<span%20style="color: #cc66cc;">0<span%20style="color: #009900;">]<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>"/><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #009900;">}<span%20style="color: #b1b100;">else <span%20style="color: #009900;">{<span%20style="color: #000000; font-weight: bold;">?><img alt="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" src="<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo catch_that_image<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"/><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #009900;">} <span%20style="color: #000000; font-weight: bold;">?></a> </div> <div > <p><span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo <span%20style="color: #990000;">mb_strimwidth<span%20style="color: #009900;">(<span%20style="color: #990000;">strip_tags<span%20style="color: #009900;">(apply_filters<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'the_content'<span%20style="color: #339933;">, <span%20style="color: #000088;">$post<span%20style="color: #339933;">-><span%20style="color: #004000;">post_content<span%20style="color: #009900;">)<span%20style="color: #009900;">)<span%20style="color: #339933;">, <span%20style="color: #cc66cc;">0<span%20style="color: #339933;">, <span%20style="color: #cc66cc;">100<span%20style="color: #339933;">,<span%20style="color: #0000ff;">"……"<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?></p> </div> <div > <a title="<span%20style="color: #000000; font-weight: bold;"><?php the_title<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>" href="<span%20style="color: #000000; font-weight: bold;"><?php the_permalink<span%20style="color: #009900;">(<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>"> 阅读全文</a> </div> </li> <span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">endforeach<span%20style="color: #339933;">; <span%20style="color: #b1b100;">endif<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>