search
HomeBackend DevelopmentPHP TutorialWordPress开发中用于获取近期文章的PHP函数使用解析_PHP

wp_get_recent_posts 函数在 WordPress 中是一个可以获取近期文章数组的函数,相较于其他的文章获取方式, wp_get_recent_posts 返回的将是一个数组而不是对象,所以在使用中对于一些新手或者是懒人,可能会更方便一些,下面我们就实例讲解一下这个函数。

描述
wp_get_recent_posts
英文解释很短,就不贴了,
中文大概意思是:获取最新文章的函数。
实际上这是一个 get_posts函数的再使用。
描述的再好也木用,下面看使用。

使用/用法

 <&#63;php 
  wp_get_recent_posts( $args ) 
 &#63;>

参数默认值

 <&#63;php $args = array(
//获取的文章数量
  'numberposts' => 10,
//从第几篇开始获取
  'offset' => 0,
//分类的ID,如果不设置则显示全部分类
  'category' => 0,
//排序规则 (注1,下详)
  'orderby' => 'post_date',
//升、降序排列
  'order' => 'DESC',
//包括的文章ID
  'include' => ,
//排除的文章ID
  'exclude' => ,
//自定义字段名称
  'meta_key' => ,
//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。
  'meta_value' =>,
//文章类型:文章或页面
  'post_type' => 'post',
//文章状态:草稿、已发布、隐藏文章等....
  'post_status' => 'draft, publish, future, pending, private',
//这个木研究
  'suppress_filters' => true ); 
&#63;>

实例
因为跟 get_posts 的使用方法实在是很相似,
所以就不给什么高深的例子了,简单的给一个官方的例子。

<h2 id="Recent-Posts">Recent Posts</h2>
<ul>
<&#63;php
 //获取最新文章
 $recent_posts = wp_get_recent_posts();
 //遍历出每一篇文章。
 foreach( $recent_posts as $recent ){
 echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .  $recent["post_title"].'</a> </li> ';
 }
&#63;>
</ul>

总结
注意:因为wp_get_recent_posts函数的返回值是一个数组,
setup_postdata 函数理论上只能接受对象类型的变量,
所以按照官方文档的解释是不能使用setup_postdata函数,装载到循环中使用的。
因为用的不多,所以我也就没有去试,有兴趣的同学可以试一试看能否成功。
所以说,如果使用 setup_postdata 函数的话,
wp_get_recent_posts 函数不如 get_posts 函数使用起来方便。

注:
‘author' —— 按作者数值编号排序
‘category' —— 按类别数值编号排序
‘content' —— 按内容排序
‘date' —— 按创建日期排序
‘ID' —— 按文章编号排序
‘menu_order' —— 按菜单顺序排序。仅页面可用。
‘mime_type' —— 按MIME类型排序。仅附件可用。
‘modified' —— 按最后修改时间排序。
‘name' —— 按存根排序。
‘parent' —— 按父级ID排序
‘password' —— 按密码排序
‘rand' —— 任意排序结果
‘status' —— 按状态排序
‘title' —— 按标题排序
‘type' —— 按类型排序

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
wordpress后台乱码怎么办wordpress后台乱码怎么办Feb 03, 2023 pm 01:48 PM

wordpress后台乱码的解决办法:1、在wordpress的“wp-admin”文件夹下找到“admin.header.php”文件;2、将“charset”属性值设置为“UTF-8”格式即可恢复正常。

如何解决wordpress标签错误问题如何解决wordpress标签错误问题Feb 03, 2023 pm 02:03 PM

wordpress标签错误的解决办法:1、找到并打开wordpress的“wp-includes”目录下的“class-wp.php”文件;2、修改内容为“$pathinfo = isset( $_SERVER['PATH_INFO'] )?mb_convert_encoding($_SERVER['PATH_INFO'],'utf-8','GBK') : '';”即可。

WordPress设置独立的Description和KeywordsWordPress设置独立的Description和KeywordsFeb 21, 2023 am 11:14 AM

你下载的WordPress主题提供的keywords和description这两个meta标签一般都做得很差,或者根本就不提供,这样不利于SEO。本文将指导你如何给主页、分类、页面以及文章页添加单独的Description 和 Keywords。

wordpress乱码怎么办wordpress乱码怎么办Mar 09, 2023 am 09:13 AM

wordpress乱码的解决办法:1、修改“wp-config.php”文件里的“define(’DB_CHARSET’, ‘utf8′);”为“define(’DB_CHARSET’, ”);”;2、把新数据库的编码设置成“latin1_swedish_ci”;3、以uft8的格式导入备份的数据库文件即可。

wordpress进不去怎么办wordpress进不去怎么办Feb 23, 2023 am 09:41 AM

wordpress进不去的解决办法:1、把地址栏“wp-login.php”后面的参数删掉,然后重新输入密码登录;2、登录FTP,下载“pluggable.php”文件,然后找到“ADMIN_COOKIE_PATH”并将它替换为“SITECOOKIEPATH”即可。

wordpress是saas吗wordpress是saas吗Feb 21, 2023 am 10:40 AM

wordpress不是saas。SaaS是一种软件销售模式,它主要针对云端应用软件,而WordPress是一款CMS系统,它主要针对网站构建和管理。虽然WordPress可以作为SaaS提供服务,但它本质上不是一种SaaS应用。

2023年最新WordPress视频教程推荐2023年最新WordPress视频教程推荐Oct 25, 2019 pm 01:12 PM

本次PHP中文网整合了相关的视频教程,中文手册,以及相关的精选文章安利给大家,统统免费!!!通过我们分享的视频,可随时随地免费观看教程视频,也不需要迅雷或者百度网盘下载了。

wordpress是哪一年的wordpress是哪一年的Feb 01, 2023 am 10:26 AM

wordpress是2003年发布的;Matt于2003年5月27日宣布推出第一版WordPress,受到了社区的欢迎,它基于b2 Cafelog并有显著改进;WordPress的第一个版本包括全新的管理界面、模板、XHTML 1.1兼容模板、内容编辑器。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),