Cooco Hub

  • 引言
  • 一、注册自定义文章类型
  • 1. 在 functions.php 中添加代码
  • 2. 代码解析
  • 二、在前端显示自定义文章类型
  • 1. 创建存档模板(archive-product.php)
  • 代码解析:
  • 2. 创建单篇文章模板(single-product.php)
  • 代码解析:
  • 三、在导航菜单中添加“产品”分类
  • 四、总结
  • Home
  • Write
  • Coding
  • Development
  • Archives
  • Comment
  • Links
  • RSS Feed
  • Link

WordPress主题开发教程 —— 第6部分:添加自定义文章类型并在前端显示

  • ☡zᶻ ☡zᶻ
  • 2025-03-24
  • 0

引言

在 WordPress 中,默认的文章类型(Post)和页面(Page)可能无法满足某些特定需求,例如网站可能需要管理“产品”、“案例”、“活动”等不同类型的内容。为了解决这个问题,WordPress 提供了“自定义文章类型(Custom Post Types,简称 CPT)”功能,让开发者可以根据需求创建新的内容类型,并提供独立的管理界面。

本教程将介绍如何在 WordPress 主题中创建自定义文章类型,并在前端模板中显示这些内容。


一、注册自定义文章类型

WordPress 允许开发者使用 register_post_type() 函数来创建新的文章类型。

1. 在 functions.php 中添加代码

打开主题目录下的 functions.php 文件,并添加以下代码:

function create_custom_post_type() {
    $labels = array(
        'name'               => __('产品', 'text_domain'),
        'singular_name'      => __('产品', 'text_domain'),
        'menu_name'          => __('产品', 'text_domain'),
        'name_admin_bar'     => __('产品', 'text_domain'),
        'add_new'            => __('添加新产品', 'text_domain'),
        'add_new_item'       => __('添加新产品', 'text_domain'),
        'new_item'           => __('新产品', 'text_domain'),
        'edit_item'          => __('编辑产品', 'text_domain'),
        'view_item'          => __('查看产品', 'text_domain'),
        'all_items'          => __('所有产品', 'text_domain'),
        'search_items'       => __('搜索产品', 'text_domain'),
        'not_found'          => __('未找到产品', 'text_domain'),
        'not_found_in_trash' => __('回收站中未找到产品', 'text_domain')
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'product'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'supports'           => array('title', 'editor', 'excerpt', 'thumbnail', 'comments')
    );

    register_post_type('product', $args);
}
add_action('init', 'create_custom_post_type');

2. 代码解析

  • $labels:定义后台管理界面中不同位置显示的文本。
  • $args:设置文章类型的参数:
    • 'public' => true:文章类型对前台和后台都可见。
    • 'has_archive' => true:启用存档页面,方便展示所有产品。
    • 'supports':定义该文章类型支持的功能,如标题、编辑器、摘要、特色图片等。
    • 'rewrite' => array('slug' => 'product'):设置文章的 URL 结构,如 yourdomain.com/product/产品名。

保存 functions.php 文件后,刷新 WordPress 管理后台,您将在“管理菜单”中看到“产品”这个新的文章类型。


二、在前端显示自定义文章类型

注册好自定义文章类型后,需要在前端页面中显示这些内容。

1. 创建存档模板(archive-product.php)

存档模板用于显示所有“产品”文章,类似于默认的 archive.php,但专用于“产品”文章类型。

在主题目录下创建 archive-product.php,并添加以下代码:

<?php get_header(); ?>

<div class="container">
    <h1><?php post_type_archive_title(); ?></h1>
    <?php if (have_posts()) : ?>
        <div class="product-list">
            <?php while (have_posts()) : the_post(); ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="product-thumbnail">
                        <?php if (has_post_thumbnail()) {
                            the_post_thumbnail('medium');
                        } ?>
                    </div>
                    <div class="product-excerpt"><?php the_excerpt(); ?></div>
                </article>
            <?php endwhile; ?>
        </div>
        <?php the_posts_pagination(); ?>
    <?php else : ?>
        <p>暂无产品。</p>
    <?php endif; ?>
</div>

<?php get_footer(); ?>

代码解析:

  • post_type_archive_title():获取并显示当前存档页面的标题。
  • have_posts() 和 the_post():循环输出所有“产品”文章。
  • the_post_thumbnail():如果文章设置了特色图片,则显示图片。
  • the_excerpt():显示文章摘要。

访问 yourdomain.com/product/,即可看到所有产品文章的列表。


2. 创建单篇文章模板(single-product.php)

当用户点击某个产品时,我们需要一个单独的页面来展示产品的详细信息。

在主题目录下创建 single-product.php,并添加以下代码:

<?php get_header(); ?>

<div class="container">
    <?php while (have_posts()) : the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h1><?php the_title(); ?></h1>
            <div class="product-thumbnail">
                <?php if (has_post_thumbnail()) {
                    the_post_thumbnail('large');
                } ?>
            </div>
            <div class="product-content"><?php the_content(); ?></div>
        </article>
    <?php endwhile; ?>
</div>

<?php get_footer(); ?>

代码解析:

  • the_title():显示产品的标题。
  • the_post_thumbnail('large'):显示大尺寸的产品图片。
  • the_content():显示产品的详细内容。

现在,点击某个产品的链接,即可访问 yourdomain.com/product/产品名/,查看详细信息。


三、在导航菜单中添加“产品”分类

默认情况下,“产品”不会出现在 WordPress 的导航菜单中。您可以手动在“外观” > “菜单”中添加它,或者使用代码自动添加。

在 functions.php 中添加以下代码,将“产品”文章类型添加到菜单:

function add_custom_post_types_to_menu($items, $args) {
    if ($args->theme_location == 'primary') {
        $items .= '<li><a href="' . get_post_type_archive_link('product') . '">产品</a></li>';
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'add_custom_post_types_to_menu', 10, 2);

这样,“产品”分类的链接将自动出现在主导航菜单中。


四、总结

在本教程中,我们学习了:

  1. 如何在 functions.php 文件中注册自定义文章类型。
  2. 如何创建 archive-product.php 存档模板来显示所有产品文章。
  3. 如何创建 single-product.php 详情页模板来展示单个产品内容。
  4. 如何在导航菜单中添加“产品”分类。

自定义文章类型是扩展 WordPress 主题的重要功能,它能帮助网站管理员管理不同类型的内容,使网站更具灵活性。在下一篇教程中,我们将介绍如何为自定义文章类型添加分类法(Taxonomies),让内容组织更加清晰。

© 2025 Cooco Hub
Theme by Wing
  • {{ item.name }}
  • {{ item.name }}