Skip to Content
使用 WordPress 构建网络应用程序,第 2 版
book

使用 WordPress 构建网络应用程序,第 2 版

by Brian Messenlehner, Jason Coleman
May 2025
Intermediate to advanced
546 pages
6h 39m
Chinese
O'Reilly Media, Inc.
Content preview from 使用 WordPress 构建网络应用程序,第 2 版

第 5 章 自定义帖子类型、帖子元数据和分类法

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

CPT 使 WordPress 成为真正的内容管理系统。 有了它们,您可以快速创建自定义功能,并以一致的方式存储数据。

默认职位类型和 CPT

在默认安装的 WordPress 中,您已经使用了几种帖子类型。您最熟悉的帖子类型可能是页面和帖子,但还有其他一些类型。这些帖子类型的值都存储在数据库wp_posts 表中,它们都使用post_type 字段来分隔。

页次

WordPress 页面用于静态内容页面,如主页、关于、联系信息、简介或任何您想要的自定义页面。 页面可以在任何层次结构中无限嵌套。它们还可以按menu_order 值排序。

职位

您的帖子就是您的博客或新闻,或者您想称其为 "不断被互联网搜索引擎收录的内容 "的任何名称。 您可以对帖子进行分类、标记关键字、设置发布日期等。一般来说,文章会以某种列表视图的形式,按时间倒序显示在网站前端。

附件

每当您在帖子中上传图片或文件时,它不仅会将文件存储在服务器上,还会将其作为帖子存储在wp_posts 表中,并带有post_type 附件。

修订

WordPress 会在您或任何人编辑文章时,将文章保存为修订版。 这项功能默认是开启的,如果途中出了差错,您可以使用它将内容恢复到原来的样子。

有时,如果您的应用程序设置为对post_content 进行大量修改, wp_posts 表就会被的修改所淹没。因此,你可能希望限制存储在wp_posts 表中的修订数量。为此,请在wp-config.php文件中添加以下代码:

define( 'WP_POST_REVISIONS', 5 );

这里,5 是指给定帖子要存储的修订帖子数。 0 的值将关闭帖子修订功能。true 或-1 的值将存储无限多的修订数(存储无限多的东西会占用大量磁盘空间)。

导航菜单项

每次使用 WordPress 核心菜单生成器(wp-admin → 外观 → 菜单)创建自定义菜单时,您都在为菜单存储包含信息的帖子。

自定义 CSS

定制器有一个 "附加 CSS "设置。该 CSS 保存在custom_css 类型的自定义帖子中。 附加 CSS 专用于活动主题,每个主题都会有一个单独的custom_css 帖子来存储 CSS。将这些附加 CSS 规则存储在自定义帖子中似乎有些奇怪,但这样做是出于性能考虑。你可以想象一下一个有大量自定义 CSS 的网站。选项有时会自动加载或缓存,而这些系统可能会因为大量 CSS 而崩溃或变慢。

更改集

更改集就像帖子修订,但针对的是自定义设置而不是帖子内容。在自定义器中更改设置时,这些更改会存储在customize_changeset 类型的帖子中。如果 您不小心关闭了浏览器标签页,然后返回到自定义器,WordPress 会提示您从更改集恢复这些更改。

嵌入式缓存

WordPress 允许您将内容的 URL 放在帖子编辑器中自己的一行中,从而嵌入来自受支持的 oEmbed 提供商的内容。如果您将 YouTube 视频的 URL 放在自己的一行,WordPress 就会检测到该 URL 是 YouTube.com 的,然后调用相应的 oEmbed URL,将 YouTube 播放器插入网站前端的帖子中。来自该请求的嵌入代码会缓存到oembed_cache 类型的自定义帖子中,并设置为您正在编辑的帖子的子帖子。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

WordPress 2: Visual QuickStart Guide

WordPress 2: Visual QuickStart Guide

Maria Langer, Miraz Jordan
Web Marketing all-in-one for Dummies®

Web Marketing all-in-one for Dummies®

Ian Lurie, Elizabeth Marsten, Michael Becker, John Arnold, Marty Dickinson
How to Develop a Great Digital Strategy

How to Develop a Great Digital Strategy

Jeanne W. Ross, Cynthia M. Beath, Ina M. Sebastian

Publisher Resources

ISBN: 9798341659339