wp_get_recent_posts 函数在 WordPress 中是一个可以获取近期文章数组的函数,相较于其他的文章获取方式, wp_get_recent_posts 返回的将是一个数组而不是对象,所以在使用中对于一些新手或者是懒人,可能会更方便一些,下面我们就实例讲解一下这个函数。
描述
wp_get_recent_posts
英文解释很短,就不贴了,
中文大概意思是:获取最新文章的函数。
实际上这是一个 get_posts函数的再使用。
描述的再好也木用,下面看使用。
使用/用法
<?php wp_get_recent_posts( $args ) ?>
参数默认值
<?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 ); ?>
实例
因为跟 get_posts 的使用方法实在是很相似,
所以就不给什么高深的例子了,简单的给一个官方的例子。
<h2 id="Recent-Posts">Recent Posts</h2> <ul> <?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> '; } ?> </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' —— 按类型排序

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment