详解WordPress开发中的get_post与get_posts函数使用_PHP
get_post()
在一般主题制作时,get_post()函数我们一般很少会用到,但因为后面会讲到get_posts(),所以我们不得不先讲一下这个单数形式。这个函数的主要作用是,将一片指定的文章以一个对象或是数组的形式返回,以便我们后期利用。下面让我们简单的了解一下他的使用方法。
get_post()函数说明
WordPress 的函数名总是那么浅显易懂,get_post()函数正如其表,即获得一篇文章,将一篇指定的文章以一个对象或是数组的形式返回,以便我们后期利用。
函数使用
<?php get_post($post_id, $output); ?>
$post_id变量,用于设置将要获取文章的ID,需要注意的是,这个变量我们不可以直接给一个实际值,那样会报错,我们只能以下面这样的形式调用。
$pid = 158; get_post($pid); //如果像get_post(158);这样调用则会报错
$post_id,默认值是none,经测试,这个变量不设置会报错,个人感觉这个变量好像没有默认值。
$output 变量用于设置返回数据的类型,有三种,对象、关联数组、数值数组。
对象:OBJECT
关联数组:ARRAY_A
数值数组:ARRAY_N
默认值:OBJECT
使用实例
<?php $mypost_id = 158; $post_id_158 = get_post($mypost_id, ARRAY_A); $title = $post_id_158['post_title']; ?>
get_posts
get_posts 函数,简单的来讲是 get_post 的复数新形势,但因为是文章多篇提取,所以使用方法上却略有不同,支持众多参数选择需要提取的文章,在 CMS 主题中经常被用到,当然如果你对 WordPress 有更深了解的话,你也有可能是用 WP_Query 来替代该函数,这就是后话了,今天主要介绍一下 get_posts 函数。
get_posts 函数详解
该函数属于 WordPress 的内置函数,用于在 WordPress 中提取多篇指定或随机文章。
越是描述简单的函数,使用起来就越是复杂,后面的需要仔细看咯!
使用方法
<?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 )将返回数组型的变量。
变量参数详解
<?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' ); ?>
注:
- ‘author' —— 按作者数值编号排序
- ‘category' —— 按类别数值编号排序
- ‘content' —— 按内容排序
- ‘date' —— 按创建日期排序
- ‘ID' —— 按文章编号排序
- ‘menu_order' —— 按菜单顺序排序。仅页面可用。
- ‘mime_type' —— 按MIME类型排序。仅附件可用。
- ‘modified' —— 按最后修改时间排序。
- ‘name' —— 按存根排序。
- ‘parent' —— 按父级ID排序
- ‘password' —— 按密码排序
- ‘rand' —— 任意排序结果
- ‘status' —— 按状态排序
- ‘title' —— 按标题排序
- ‘type' —— 按类型排序
实例
刚我们讲到用数组去传参,当然我们也可以用字符串来给该函数传参,下面给一个简单的例子。
<?php $posts_rand = get_posts('numberposts=3&orderby=rand'); ?>
以上代码用于随机在 WordPress 中获取3篇文章。
总结
其实 query_posts() 和 get_posts() 函数,接受大部分的参数,使用同样结构的数据库查询语句,并能达到一样的目的,但部分主题作者提示 query_posts()有可能会扰乱 WordPress 主循环,所以在这里不推荐使用。
get_posts 使用不难,难在获取文章后怎样在页面中显示,这里就需要有一定的 PHP 知识了。
如果是在文章循环之外想要将查询内容显示出来,可以看一下setup_postdata这个函数,这个函数会给那些用惯了模板标签的童鞋很大的帮助。

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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.

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.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >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.

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.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to
