详解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



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

Reasons why WordPress websites cannot be found in search engines: 1. Indexing issues; 2. Content issues; 3. Website technical issues; 4. Link issues; 5. Other issues such as geographical restrictions, website name and social media presence.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

Be cautious when building a WordPress website. The guide to breaking through pits helps you avoid risks: choose paid themes and avoid the quality and safety risks of free themes. "Less is more" when installing plugins to avoid website speed and compatibility issues. Regularly optimize the database to ensure the smooth operation of the website. Pay attention to security measures and regularly update and install security plug-ins. Modify the code carefully to avoid website crashes and do it in a test environment if necessary. Pay attention to performance optimization, improve website speed, and improve user experience.

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

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.
