You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中创建新的自定义文章类型?请指导操作方法

Hey Mike, no worries at all—creating custom post types (CPTs) in WordPress is totally manageable, and I’ll break down two reliable methods for you, depending on how comfortable you are with code.

方法1:手动添加代码(推荐,长期稳定)

This is the best approach if you want full control and don’t want to rely on plugins. Just follow these steps:

  1. Access your theme’s (or child theme’s) functions.php file

    • You can do this via your WordPress dashboard (Appearance → Theme File Editor) or through FTP/SFTP. Pro tip: Always use a child theme! If you edit your parent theme’s functions.php, your changes will get wiped when the theme updates.
  2. Add the custom post type registration code
    Paste this snippet at the bottom of the file. Let’s use a "Book" CPT as an example—you can tweak the labels and settings to match your needs:

    function create_book_cpt() {
        $args = array(
            'labels' => array(
                'name' => 'Books',
                'singular_name' => 'Book',
                'add_new' => 'Add New Book',
                'add_new_item' => 'Add New Book',
                'edit_item' => 'Edit Book',
                'new_item' => 'New Book',
                'view_item' => 'View Book',
                'search_items' => 'Search Books',
                'not_found' => 'No books found',
                'not_found_in_trash' => 'No books found in Trash'
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'books'),
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
            'menu_icon' => 'dashicons-book-alt', // Use a WordPress dashicon
        );
        register_post_type('book', $args);
    }
    add_action('init', 'create_book_cpt');
    
    • Quick breakdown of key settings:
      • public: Makes the CPT visible on the frontend and backend
      • has_archive: Enables an archive page (e.g., yoursite.com/books)
      • supports: Defines which WordPress features the CPT uses (title, editor, etc.)
      • menu_icon: Adds a custom icon to the WordPress admin menu (check WordPress dashicons for options)
  3. Refresh your WordPress dashboard
    You should now see a new "Books" menu item in the left sidebar.

  4. Fix permalinks (critical!)
    Go to Settings → Permalinks and just click "Save Changes" (no need to edit anything). This ensures your CPT’s single posts and archive page load correctly instead of showing a 404 error.

方法2:使用插件(适合新手,无需代码)

If you’re not comfortable editing code, a plugin makes this super easy:

  1. Install and activate Custom Post Type UI

    • Go to Plugins → Add New in your dashboard
    • Search for "Custom Post Type UI"
    • Install and activate the plugin
  2. Create your custom post type

    • Navigate to CPT UI → Add/Edit Post Types
    • Fill in the basic details:
      • Post Type Slug: A lowercase, hyphenated identifier (e.g., book)
      • Plural Label: The plural name (e.g., Books)
      • Singular Label: The singular name (e.g., Book)
    • Click "Populate Additional Labels" to auto-fill the rest of the label fields, or tweak them manually.
  3. Configure advanced settings

    • Under "Settings", check options like "Public", "Has Archive", and select which features you want to support (title, editor, etc.)
    • You can also set a custom menu icon here.
  4. Save and check your dashboard
    Click "Add Post Type", then refresh your dashboard—your new CPT menu will be there. Don’t forget to refresh your permalinks (same step as method 1!) to avoid 404s.

Quick Tips
  • If you need custom fields for your CPT (like author name, publication date for books), you can use the Advanced Custom Fields plugin (search for it in the plugin directory)
  • For code-based CPTs, consider creating a small custom plugin instead of using functions.php—it’s even safer if you switch themes later
  • Always test changes on a staging site first if you’re working on a live site!

内容的提问来源于stack exchange,提问作者Mike Jessay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:16:27