当前位置:首页 > 网络科技 > WordPress 通过文章标题获取文章 ID 等 post 信息

WordPress 通过文章标题获取文章 ID 等 post 信息

1年前 (2024-08-31)admin网络科技76

WordPress 可以通过文章 ID 获取到指定文章的数据,也可以通过 URL 获取文章 ID,但是最近子凡在开发一个泪雪网的批量数据理解功能,需要将相关标题的文章 URL 做批量提交,那么就需要用到 WordPress 的一个自带函数 get_page_by_title。

WordPress 默认也已经提供了可以通过文章标题就能够快速获取到文章信息的函数“get_page_by_title”,作用和 get_post 函数功能差不多,只是查询的条件不一样,返回的结果其实就是 get_post 处理过的数据。

1
2
3
4
5
6
7
8
9
10
//格式
get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' )
 
$page_title 页面标题
$output 返回的类型格式,例如 OBJECT,ARRAY_A 或 ARRAY_N 选一个,默认是 OBJECT
$post_type 文章类型,默认为 page 页面,查询文章为 post
 
//WordPress 通过文章标题获取 post 信息
$post = get_page_by_title('这里就是文章的标题', OBJECT, 'post');
$postid = $post->ID; //文章 ID

详细的 post 返回字段包括:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ID          int     文章 ID
post_author     string  作者 user ID
post_name   string  文章别名
post_type   string  文章类型
post_title  string  文章标题
post_date   string  文章时间,格式为: 0000-00-00 00:00:00
post_date_gmt   string 文章 gmt 时间,格式为,: 0000-00-00 00:00:00
post_content    string  文章内容
post_excerpt    string  文章摘要
post_status     string  文章状态
comment_status  string  评论状态,开启或者关闭: { open, closed }
ping_status     string  ping 状态 { open, closed }
post_password   string  文章密码
post_parent     int     父 ID,默认是 0
post_modified   string  文章修改时间,格式为: 0000-00-00 00:00:00
post_modified_gmt   string  文章修改时间,格式为: 0000-00-00 00:00:00
comment_count   string  文章评论统计
menu_order  string  好像没什么用,默认是 0

好啦,简单的做个记录和分享,相信对于一些新人 WordPress 开发者会有一点点帮助吧,可以竟可能的避免重复造轮子的情况,利用好 WordPress 已经提供好的函数,可以让开发效率事半功倍。

更多关于WordPress优化及疑问可以添加留言

w.haolusi.com

本文链接:https://w.haolusi.com/wordpress-get_page_by_title.html

扫描二维码推送至手机访问。

版权声明:本文由豪鲁斯兴趣网发布,如需转载请注明出处。

本文链接:https://w.haolusi.com/?id=2285

标签: WordPress优化
分享给朋友:

“WordPress 通过文章标题获取文章 ID 等 post 信息” 的相关文章

The latest podcast industry trends and predictions

The latest podcast industry trends and predictions

_ Latest Trends1. **Content Diversification**:- Podcast content is becoming increasingly diversified, covering everything from news, educati...

MYSQL字符集有哪些

MYSQL字符集有哪些

MySQL 中的字符集是用来确定数据库中字符数据的编码方式,它决定了如何存储和检索数据。MySQL 中常用的字符集:UTF8:UTF-8 是一种 Unicode 字符编码方式,它可以表示世界上大部分的文字字符。MySQL 中的 UTF8 字符集最多只能存储 3 字节的 UTF-8 编码字符,...

最新免费可用!ChatGPT 4.0/4o/3.5 镜像 Mirror|免翻直链中文镜像(2024年8月更新)

最新免费可用!ChatGPT 4.0/4o/3.5 镜像 Mirror|免翻直链中文镜像(2024年8月更新)

本篇文章目录|Table of Contents Hide ChatGPT 4.0/4o/3.5 镜像站列表ChatGPT镜像更新历史什么是Cha...

最新可用!2024年Google谷歌镜像,Google学术镜像站(8月更新)

最新可用!2024年Google谷歌镜像,Google学术镜像站(8月更新)

本篇文章目录|Table of Contents Hide Google谷歌镜像-直接访问谷歌搜索01.Google谷歌搜索最新镜像入口02.Go...

WordPress别名slug增加点(.)符号支持

WordPress别名slug增加点(.)符号支持

熟悉 WordPress 的朋友们都知道,WordPress 在 URL 链接中的处理算得上是一刀切,所有的特殊符号及字符都是使用减号/连字符(-)来进行分隔,这其实是一种对搜索引擎非常友好的分隔方式,也是谷歌搜索引擎所推荐的连字符,其中谷歌有一句提示“不妨考虑在网址中使用连字符来分隔字...

FCPX/PR的剪辑时间线XML与字幕SRT互转|字幕Srt转XML互转工具|xml转srt|srt转fcpxml|Tutorial

FCPX/PR的剪辑时间线XML与字幕SRT互转|字幕Srt转XML互转工具|xml转srt|srt转fcpxml|Tutorial

本篇文章目录|Table of Contents Hide 概述应用场景FCPX/PR剪辑时间线XML与SRT字幕互转获取与安装:直接安装 ...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。