WordPress 帖子类型:完整指南
已发表: 2021-02-16如果您是那种非常关心设计和组织的 WordPress 开发人员,那么是时候全面了解 WordPress 帖子类型了。
众所周知,并非所有 WordPress 网站都是简单的博客。 通常,开发人员需要的不仅仅是简单、标准的页面和帖子。 他们需要能够添加全新的内容类型。
但是,打破 WordPress 设计和开发的玻璃天花板,同时仍为用户提供出色的 UX 的最佳方法是什么?
答案可以在 WordPress 帖子类型和 WordPress 自定义帖子类型中找到。
通过学习、理解和实现 WordPress 帖子类型的全部潜力,您将把您的管理区域变成一个令人兴奋的、有组织的和个性化的地方,完全属于您自己。
WordPress 帖子类型允许设计人员通过创建新的存储桶来放置您独特的内容类型来轻松组织内容。
但这究竟是什么意思,你从哪里开始呢?
使用 WordPress 自定义帖子类型可以为您网站的整体用户体验做什么?
让我们来了解一下。
什么是 WordPress 帖子类型?
在最基本的范围内,WordPress 帖子类型的工作方式类似于您管理区域中熟悉的帖子和页面。
默认情况下,WordPress 核心包含几种不同的内容类型,这些内容类型分为帖子类型。 这些内容类型之一称为帖子。 但是,这只是 WordPress 中众多标准帖子类型中的一种。
当您使用 WordPress 核心时,它会自动附带以下帖子类型,每种类型都存储在 WordPress 数据库的 wp_posts 表中:
- 页面
- 帖子
- 修订
- 附件
- 自定义 CSS
- 导航菜单
- 变更集
这些帖子类型中的每一种都有编辑器和标题字段,就像您习惯使用常规页面和帖子一样。
当您创建自定义帖子类型时(我们将在本指南中向您展示),它会像标准 WordPress 帖子类型一样出现在您的 WordPress 管理菜单中。
一旦您实现了 WordPress 自定义帖子类型,您就可以像在帖子部分添加新的博客内容一样向其中添加内容。
但是,使用 WordPress 帖子类型,您可以使这些帖子类型比标准帖子和页面做得更多。 您还可以自定义站点前端向站点用户显示帖子类型内容的方式。
新的帖子类型可以是您想要的任何内容。 根据您网站的主题,它可能是这样的:
- 家园
- 评论
- 实例探究
- 电影
- 励志名言
- 等等。
WordPress 帖子类型功能使任何新类型的内容成为可能。
当然,有一些插件可以帮助您创建 WordPress 帖子类型。 但是您可能并不一定希望插件能够对您的网站设计和功能进行如此多的控制。
如果您想完全控制您的设计和用户体验,最好的选择是编写您自己的自定义帖子类型。
我们将在本指南的后面为那些喜欢该选项来创建 WordPress 自定义帖子类型的人推荐一个插件。
但注册、添加或创建 WordPress 帖子类型的最佳方式是将其直接编码到您网站的主题中。 这是通过使用 WordPress register-post-type 功能完成的。
通过使用此功能,您可以立即将内容添加到新的帖子类型存储桶中,并将其显示在您的网站上。
更好的是,它只需要添加五行代码即可开始。
看看 WordPress 自定义帖子类型
现在您已经对 WordPress 帖子类型和自定义帖子类型有了基本的了解,现在是时候动手了。
首先,请按照以下步骤创建新的帖子类型:
- 登录到您的管理区域。
- 打开您的代码编辑器和主题的functions.php 文件。
- 将下面显示的五行代码添加到functions.php 文件的顶部。
- 刷新浏览器。
- 检查新帖子类型菜单项的评论下的 WordPress 管理菜单。
- 刷新你的永久链接。
这是要添加的代码:
<?php
add_action( 'init', function() {
$label = 'Books';
$type = 'book';
register_post_type( $type, [ 'public' => true, 'label' => $label ] );
});
您现在应该有一个新的帖子类型。
但是你实际上可以用新的帖子类型做什么?
WordPress 帖子类型和 WordPress 自定义帖子类型的力量
为了真正深入研究帖子类型,让我们使用一个具体示例来创建一个新的投资组合网站,该网站需要包含一系列案例研究。
为此,我们将注册一个新的帖子类型来管理每个案例研究。
使用默认的二十二十 WordPress 主题,我们将首先将我们的研究帖子类型添加到主题中。
首先,在您的代码编辑器中打开二十二十主题的functions.php 文件。 这是您编写帖子类型代码的地方。
新的帖子类型必须在functions.php 文件的顶部注册。 这是通过使用 add_action() 的 init WordPress 钩子完成的。 当您使用函数 register_post_type 添加帖子类型时,如果没有 init 钩子,它将无法正常工作。
然后,从 register_post_type 的三个参数开始:
- 公开 - 这意味着设置帖子类型,以便每个人都可以访问它。
- 描述 - 这不是经常使用,但它仍然很好。
- 标签——这是一个我们稍后会扩展的论点。
<?php
add_action( 'init', function() {
$type = 'study';
$label = 'Studies';
$arguments = [
'public' => true, // Allow access to post type
'description' => 'Case studies for portfolio.', // Add a description
'label' => $label // Set the primary label
];
register_post_type( $type, $arguments);
});
确保每次完成更改时刷新永久链接。
自定义帖子类型标签
默认情况下,WordPress 将在您网站的整个管理区域中将新帖子类型标记为 Post。 但您可能希望将标签标记为名为 Study 而不是 Post 的新帖子类型。
为了覆盖帖子类型的默认标签,您需要手动调用它们。
要正确设置标签,请创建一个新函数来编译标签,而不会弄乱您的帖子类型注册代码。
只需将以下功能添加到您的主题中,即可在帖子类型注册过程中使用:
<?php
function xcompile_post_type_labels($singular = 'Post', $plural = 'Posts') {
$p_lower = strtolower($plural);
$s_lower = strtolower($singular);
return [
'name' => $plural,
'singular_name' => $singular,
'add_new_item' => "New $singular",
'edit_item' => "Edit $singular",
'view_item' => "View $singular",
'view_items' => "View $plural",
'search_items' => "Search $plural",
'not_found' => "No $p_lower found",
'not_found_in_trash' => "No $p_lower found in trash",
'parent_item_colon' => "Parent $singular",
'all_items' => "All $plural",
'archives' => "$singular Archives",
'attributes' => "$singular Attributes",
'insert_into_item' => "Insert into $s_lower",
'uploaded_to_this_item' => "Uploaded to this $s_lower",
];
}
然后,在注册码中,您需要调用我们刚刚创建的函数 xcompile_post_type_labels()。 将它用于您的标签,然后检查您的管理区域。
<?php
add_action( 'init', function() {
$type = 'study';
// Call the function and save it to $labels
$labels = xcompile_post_type_labels('Study', 'Studies');
$arguments = [
'public' => true,
'description' => 'Case studies for portfolio.',
'labels' => $labels // Changed to labels
];
register_post_type( $type, $arguments);
});
到目前为止一切如何?
WordPress 自定义帖子类型的菜单图标和位置
自从引入 WP Dashicons 以来,将菜单图标添加到帖子类型变得非常容易。
为此,首先将 menu_icon 参数设置为 Dashicon 名称。 对于此示例,我们将通过使用值 dashicons-desktop 来使用计算机桌面图标。
<?php
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
$arguments = [
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop', // Set icon
'labels' => $labels
];
register_post_type( $type, $arguments);
});
现在,如果您想更改菜单项的位置,请使用 menu_position 参数。 此参数将采用从 0 开始并以 100 结束的值。所选值将在菜单基础上列出更高或更低的菜单项,0 值位于顶部,100 位于底部。
现在,我们应该向新的帖子类型添加特色图片。
如何启用和禁用特色图像、标题和编辑器
要添加或更改帖子类型的默认管理表单字段,例如特色图片、标题和编辑器字段,请使用 set support 参数。
您可以使用帖子类型支持参数打开和关闭许多选项。 他们是:
- 标题字段
- 编辑
- 作者框
- 缩略图(特色图片)
- 摘抄
- 引用
- 自定义字段
- 注释
- 修订
- 页面属性
- 后期格式
对于您的新研究帖子类型,您可以覆盖默认的 WordPress 选项并通过应用缩略图选项添加您自己的特色图片。
在执行此操作之前,请确保您已启用对后缩略图的主题支持。 这是通过使用 add-theme-support 功能完成的。
只需添加几行代码即可使用支持功能,并且将启用特色图像。
<?php
// Add theme support for featured image / thumbnails
add_theme_support('post-thumbnails');
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
// Declare what the post type supports
$supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];
$arguments = [
'supports' => $supports, // Apply supports
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
没有多少 WordPress 设计师或开发人员深入了解 WordPress 自定义帖子类型。 现在是轻拍自己的后背的好时机。
WordPress 自定义帖子类型的层次结构
虽然 WordPress 帖子不能有子帖子,但页面可以。 页面的子页面被视为子页面。
如果您愿意,您可以使用层次参数让 WordPress 自定义帖子类型使用类似页面的层次结构。
对于我们的研究帖子类型示例,我们不一定需要这个。 但是,如果您的特定项目需要它,这是要使用的代码:
<?php
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
// Declare what the post type supports
$supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];
$arguments = [
'hierarchical' => false, // Do not use hierarchy
'supports' => $supports,
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
如何为新的帖子类型启用 REST API
REST API 永远改变了 WordPress。 因此,您会希望您的新帖子类型能够访问 WordPress 的所有最新功能,例如 Gutenberg。
为新的 WordPress 自定义帖子类型启用 REST API 就像将 show_in_rest 参数设置为 true 一样简单。
启用 REST API 后,您的新帖子类型将开始使用 Gutenberg(如果它也支持编辑器)。
<?php
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
// Declare what the post type supports
$supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];
$arguments = [
'show_in_rest' => true, // Enable the REST API
'hierarchical' => false,
'supports' => $supports,
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
当您在 WordPress 中启用 REST API 时,您还可以通过特定的 WordPress 端点以 JSON 对象的形式访问新的帖子类型。

要查看帖子类型 REST 的端点,请使用此 /wp-json/wp/v2/study。
要将 URL 中的帖子类型基本名称更改为研究而不是研究,请使用 rest_base 参数。
<?php
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
// Declare what the post type supports
$supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];
$arguments = [
'rest_base' => 'studies', // Change the REST base
'show_in_rest' => true,
'hierarchical' => false,
'supports' => $supports,
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
就像这样,设置了 API 并在帖子类型上启用了古腾堡。
如何将古腾堡添加到 WordPress 自定义帖子类型
请记住,除非 Gutenberg 支持编辑器并启用了 REST API,否则无法为自定义帖子类型启用。
启用后,要将 Gutenberg 添加到您的帖子类型,我们需要暂时忘记我们的 Study 帖子类型并创建一个新的帖子类型,我们将其称为文章。
我们将使用文章帖子类型来启用古腾堡。
<?php
add_action( 'init', function() {
$type = 'article';
$labels = xcompile_post_type_labels('Article', 'Articles');
$arguments = [
'rest_base' => 'articles',
'show_in_rest' => true, // Required for Gutenberg
'supports' => ['editor'], // Required for Gutenberg
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
您会注意到需要添加 Gutenberg 的两个参数是:
'show_in_rest' => true,
'supports' => ['editor'],
如何从 WordPress 自定义帖子类型中删除古腾堡
如果您想从您的一种帖子类型中删除 Gutenberg,只需将操作钩子 use_block_editor_for_post_type 添加到您主题的 functions.php 文件中。
这是通过以下代码完成的:
<?php
add_filter('use_block_editor_for_post_type', function($enabled, $post_type) {
// List of post types to remove
$remove_gutenberg_from = ['study'];
if (in_array($post_type, $remove_gutenberg_from)) {
return false;
}
return $enabled;
}, 10, 2);
这会从帖子类型中强制禁用古腾堡。
请注意,并非所有 WordPress 自定义帖子类型都应启用 Gutenberg。 有时您可能希望使用带有帖子类型的经典 WordPress 编辑器。
当需要从 REST API 访问帖子类型时,禁用古腾堡功能也会派上用场。
在这些情况下,禁用古腾堡是您想要的方向。
启用档案和设置前端
我们终于到了解决 WordPress 自定义帖子前端设计的时候了。
当您想要设置 WordPress 自定义帖子类型内容列表时,您需要做三件事:
- 启用 has_archive 参数。
- 将重写规则 slug 设置为帖子类型名称的复数形式。 在本指南的示例中,需要将其更改为“研究”。
- 刷新你的永久链接。
<?php
add_action( 'init', function() {
$type = 'study';
$labels = xcompile_post_type_labels('Study', 'Studies');
// Declare what the post type supports
$supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];
$arguments = [
'rewrite' => [ 'slug' => 'studies' ] // Change the archive page URL
'has_archive' => true, // Enable archive page
'rest_base' => 'studies',
'show_in_rest' => true,
'hierarchical' => false,
'supports' => $supports,
'public' => true,
'description' => 'Case studies for portfolio.',
'menu_icon' => 'dashicons-desktop',
'labels' => $labels,
];
register_post_type( $type, $arguments);
});
完成所有这些后,是时候开始为自定义帖子类型的内容设置主题了。 现在,我们将使用默认的二十二十主题。 当然,您可以使用任何您喜欢的主题。
WordPress 自定义帖子类型模板
在我们对自定义帖子类型进行模板化之前,让我们先看看 WordPress 模板层次结构。
如果您还不熟悉 WordPress 处理模板的方式,请将其视为类似于 CSS 处理特殊性的方式。 虽然陷入细节并不太重要,但这个简单的类比将帮助您更好地理解 WordPress 模板层次结构。
在我们新的研究帖子类型的模板层次结构中,有两个特定的模板文件需要注意。
他们是:
1. single-study.php – 当用户导航到 http://yourcoolsite.com/studies/your-cool-study-post 以查看单个研究时,这是针对单个页面的。
2. archive-study.php – 当用户导航到 http://yourcoolsite.com/studies 以查看所有研究的列表时,这是存档页面。
默认情况下,WordPress 将使用主题的 single.php 和 archive.php 模板进行自定义帖子类型的前端设计。 但是,当自定义帖子类型存在特定模板时, single-study.php 和 archive-study.php 将覆盖 WordPress 中的默认值。
单页
由于我们在本指南中使用默认的 WordPress 二十二十主题,因此我们首先需要创建 single-study.php 的文件名并使用它。
在单个帖子类型模板文件中,添加此 WordPress 循环:
<?php
/**
* The template for displaying single posts and pages.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Twenty
* @since 1.0.0
*/
get_header();
?>
<main id="site-content" role="main">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
}
}
?>
</main><!-- #site-content -->
<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
<?php get_footer(); ?>
存档页面
帖子类型的存档页面使用与单个页面相同的完全过程。 唯一的区别是您将使用archive-study.php 模板而不是single-study.php。
对于您的存档页面,您会希望它链接到每项研究,并且只显示“研究”的标题。
当然,您可以随意使用此代码享受自己的乐趣:
<?php
/*
Template Name: Archive-Study
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php get_search_form(); ?>
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
现在您的存档页面已经完美到位,看起来您已经完成了。 但是如果你有一百个或更多的案例研究会发生什么?
如何修改存档页面查询
存档页面只会列出管理员在“设置”>“阅读”中指定的项目数。 通常,您不希望对自定义帖子类型应用与主要博客或文章提要相同的限制。
当您想删除存档页面上的限制时,只需使用钩子 pre_get_posts 修改主查询。
在您连接到 pre_get_posts 之后,您将能够访问和修改主要的 WP_Query 对象。
为了在您的档案页面上列出所有研究,您需要:
- 使用钩子 pre_get_posts。
- 检测方法 is_main_query 是否正在运行主查询。
- 使用 is_post_type_archive 检测您是否在研究存档页面上。
- 在主 WP_Query 中,将 posts_per_page 设置为 -1。 这将删除所有数量限制并列出所有内容。
<?php
add_action('pre_get_posts', function( WP_Query $query ) {
if($query->is_main_query() && $query->is_post_type_archive('study')) {
$query->set('posts_per_page', -1);
}
});
通过使用钩子 pre_get_posts,您可以直接访问 WordPress 已经在运行的 SQL 查询。 该查询使您无需编写任何 SQL 即可访问帖子、页面和自定义帖子类型。
这总是一个奖励。
为 WordPress 自定义帖子类型使用插件

如果您不是深入研究代码的类型,而是希望使用插件创建和修改自定义帖子类型,自定义帖子类型 UI 提供了一个易于使用的界面,用于注册和管理自定义帖子类型(和分类法)。
该插件受到高度评价,并配置为与 WordPress 5.5 及更高版本一起使用。
它已经被测试到 5.6。
使用生成器生成 WordPress 自定义帖子类型
想要一种非常简单的方法来获取自定义帖子类型(或分类法)的代码? 查看 GenerateWP。 您可以使用 GenerateWP 创建范围广泛的自定义代码。 这也很简单。 只需填写正确的表格并获取要复制/粘贴的代码。
您甚至可以看到几年前以 GenerateWP 为特色的关于自定义帖子类型的培训网络研讨会。 好消息是自定义帖子类型已经存在很长时间了,培训仍然是相关的。
WordPress 帖子类型是您最好的新朋友
无论您是手动使用 WordPress 帖子类型还是使用插件,让您的站点配备可用的最佳 WordPress 备份插件都非常重要。
编辑代码总是有可能导致冲突,这可能会使您的网站朝着您不希望的方向发展。
在这些情况下,BackupBuddy 和 iThemes Security,一个强大的 WordPress 安全插件,将帮助您安全地恢复工作。
AJ 拥有超过 20 年的与广泛客户合作和开发会员网站的经验。
