在 WordPress 中,Post Type(文章类型)是指不同类型的内容,例如文章(Post)、页面(Page)、自定义文章类型等。每种文章类型可以使用不同的模板来显示其内容。以下是关于 WordPress 文章类型模板的基本信息和操作方法:
默认文章类型
WordPress 默认有以下两种文章类型:
Post(文章):用于博客文章。
Page(页面):用于静态页面。
它们的模板文件通常位于主题文件夹中,例如:
single.php:用于显示单篇文章。
page.php:用于显示单个页面。
自定义文章类型
如果你通过代码或插件创建了自定义文章类型(Custom Post Type),可以通过创建特定的模板文件来控制其显示方式。
自定义文章类型模板命名规则
WordPress 会根据模板文件的名称自动匹配对应的文章类型。命名规则如下:
single-{posttype}.php:用于显示单个自定义文章类型的内容。
例如,如果你的自定义文章类型是 product,则模板文件命名为 single-product.php。
archive-{posttype}.php:用于显示自定义文章类型的归档页面。
例如,archive-product.php 用于显示所有产品的列表。
如果没有找到特定模板文件,WordPress 会默认使用 single.php 或 archive.php。
单个内容页模板示例 (single-product.php)
<?php
/**
* The template for displaying all single posts of type 'product'.
*/
get_header(); ?>
<main id="primary" class="site-main">
<?php
while (have_posts()) :
the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title('<h1 class="entry-title">', '</h1>'); ?>
</header>
<div class="entry-content">
<?php
// 显示内容
the_content();
// 显示自定义字段(如果需要)
if (get_field('price')) {
echo '<p>Price: ' . get_field('price') . '</p>';
}
?>
</div>
</article>
<?php
endwhile;
?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
注册一个自定义文章类型
function create_product_post_type() {
// 定义标签
$labels = array(
'name' => __('产品', 'textdomain'), // 文章类型名称
'singular_name' => __('产品', 'textdomain'), // 单篇文章类型名称
'menu_name' => __('产品', 'textdomain'), // 后台菜单名称
'name_admin_bar' => __('产品', 'textdomain'), // 后台工具栏名称
'add_new' => __('添加新产品', 'textdomain'), // 添加新文章
'add_new_item' => __('添加新产品', 'textdomain'), // 添加新文章页面标题
'new_item' => __('新产品', 'textdomain'), // 新文章
'edit_item' => __('编辑产品', 'textdomain'), // 编辑文章
'view_item' => __('查看产品', 'textdomain'), // 查看文章
'all_items' => __('所有产品', 'textdomain'), // 所有文章
'search_items' => __('搜索产品', 'textdomain'), // 搜索文章
'parent_item_colon' => __('父级产品:', 'textdomain'), // 父级文章
'not_found' => __('未找到产品', 'textdomain'), // 未找到文章
'not_found_in_trash' => __('回收站中未找到产品', 'textdomain') // 回收站中未找到文章
);
// 定义参数
$args = array(
'labels' => $labels, // 标签
'public' => true, // 是否公开
'publicly_queryable' => true, // 是否可以通过前端查询
'show_ui' => true, // 是否显示后台管理界面
'show_in_menu' => true, // 是否显示在后台菜单中
'query_var' => true, // 是否允许通过查询变量查询
'rewrite' => array('slug' => 'product'), // 自定义 URL 别名
'capability_type' => 'post', // 权限类型
'has_archive' => true, // 是否有归档页面
'hierarchical' => false, // 是否支持层级结构
'menu_position' => 5, // 菜单位置(5 表示在“文章”下方)
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'), // 支持的功能
'menu_icon' => 'dashicons-cart', // 菜单图标(使用 Dashicons)
);
// 注册文章类型
register_post_type('product', $args);
}
add_action('init', 'create_product_post_type');
labels:定义了文章类型在后台的各种标签和文本。
args:定义了文章类型的行为和特性。
public:是否公开,设置为 true 表示可以在前台显示。
has_archive:是否启用归档页面。
supports:支持的功能,例如标题、编辑器、特色图片、摘要等。
menu_icon:后台菜单图标,可以使用 WordPress 自带的 Dashicons。
rewrite:自定义 URL 别名,例如 example.com/product/sample-product。
关于层级结构
hierarchical 参数的作用
hierarchical 设置为 true:
文章类型支持层级结构,类似于页面(Page)。可以为文章设置父级和子级关系。在编辑文章时,会显示“父级”选项,允许你选择父级文章。
hierarchical 设置为 false:文章类型不支持层级结构,类似于文章(Post)。无法为文章设置父级和子级关系。在编辑文章时,不会显示“父级”选项。
如果文章类型不需要层级结构,建议将 hierarchical 设置为 false,以避免不必要的复杂性。启用层级结构后,确保模板文件能够正确处理父子级关系。