Table of Contents
WordPress开发中用于获取近期文章的PHP函数使用解析,
Recent Posts
您可能感兴趣的文章:
Home php教程 php手册 WordPress开发中用于获取近期文章的PHP函数使用解析,

WordPress开发中用于获取近期文章的PHP函数使用解析,

Jun 13, 2016 am 08:48 AM
wordpress article

WordPress开发中用于获取近期文章的PHP函数使用解析,

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

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

使用/用法

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

参数默认值

 <&#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;>
Copy after login

实例
因为跟 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>
Copy after login

总结
注意:因为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' —— 按类型排序

您可能感兴趣的文章:

  • WordPress开发中自定义菜单的相关PHP函数使用简介
  • WordPress中用于获取搜索表单的PHP函数使用解析
  • 在WordPress中使用wp_count_posts函数来统计文章数量
  • 详解WordPress中调用评论模板和循环输出评论的PHP函数
  • 在WordPress中加入Google搜索功能的简单步骤讲解
  • 详解WordPress开发中的get_post与get_posts函数使用
  • 解析WordPress中的post_class与get_post_class函数
  • WordPress开发中的get_post_custom()函数使用解析
  • 在WordPress中安装使用视频播放器插件Hana Flv Player
  • 详解WordPress中分类函数wp_list_categories的使用
  • WordPress开发中短代码的实现及相关函数使用技巧
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

See all articles