Plugin mẫu bài đăng tùy chỉnh WordPress

wordpress 4. 7 đã cung cấp một số cải tiến mới tuyệt vời cho hệ thống mẫu của CMS. Các mẫu hiện đã được mở rộng để bao gồm tất cả các loại bài đăng, cho phép nhà phát triển tạo nhiều chủ đề đa sắc thái hơn, đồng thời cho phép chủ sở hữu trang web quản lý nội dung dễ dàng hơn

Nhưng làm thế nào để tất cả hoạt động?

Tiếp tục đọc hoặc tiếp tục sử dụng các liên kết này

Mẫu trong WordPress là gì?

Mẫu về cơ bản là các tệp đặc biệt có thể thay đổi giao diện của trang và/hoặc thêm chức năng cho trang web WordPress của bạn

Trước WordPress 4. 7 mẫu chỉ có sẵn cho các trang, do đó chúng được mệnh danh là mẫu trang. Tuy nhiên, WordPress 4. 7 đã mở rộng việc sử dụng chúng để bao gồm bất kỳ loại bài đăng nào, vì vậy tôi đổi tên chúng thành “mẫu” cho ngắn gọn

Plugin mẫu bài đăng tùy chỉnh WordPress
Bộ chọn mẫu WordPress trong Trình chỉnh sửa bài đăng

Sau khi các mẫu được đăng ký trong chủ đề, chúng có thể được chọn trên các trang chỉnh sửa bài đăng

Đặt nền móng cho việc sử dụng các mẫu

Hãy làm việc trên một ví dụ

Để kiểm tra điều này, trước tiên chúng ta nên tạo một chủ đề con. Hãy sử dụng Twenty Seventeen, chủ đề mặc định mới của WordPress

Để bắt đầu, hãy tạo một thư mục mới trong thư mục chủ đề của bạn. Tôi đặt tên cho mình là “cppt” (viết tắt của các mẫu loại bài đăng tùy chỉnh) và sau đó tôi đã thêm các tệp mới mới. Phong cách. css và các chức năng. php. Sử dụng mã bên dưới để thiết lập các tệp này

Đang tải ý chính 7f262ddd286bd9c587096dffd2da43a4

Đang tải ý chính f3a0f3d48211cb2a04bc8c9635ec97d0

Tất cả các thiết lập. Bây giờ bạn sẽ thấy chủ đề con trong phần Giao diện của quản trị viên WordPress của bạn. Sau khi được kích hoạt, bạn sẽ thấy trải nghiệm Twenty Seventeen màu vani, chưa sửa đổi

Tạo loại bài đăng tùy chỉnh

Hãy kiểm tra hệ thống mẫu mới với loại bài đăng tùy chỉnh. Hãy tưởng tượng bạn đang tạo một trang web về trò chơi, như IGN hoặc Gamespot

Trong khi viết về trò chơi, bạn có thể xuất bản các mục tin tức, đó là các phần ngắn hơn và các bài đánh giá đầy đủ, cung cấp cái nhìn sâu sắc về trò chơi

Để làm cho cái này trông khác với cái kia, bạn cần sử dụng một danh mục hoặc một số thiết bị tùy ý khác;

Đang tải ý chính dffbe9c201e10dfec08b06bcd4aca180

Tôi đã bỏ qua nhiều nhãn và tùy chọn và mã cơ bản này sẽ phù hợp với mục đích của chúng tôi

Tạo mẫu

Mẫu là một tệp PHP thông thường có nhận xét đặc biệt ở trên cùng, cho WordPress biết tiêu đề của mẫu là gì và có thể sử dụng nó ở đâu

Hãy tạo một cái để đánh giá trò chơi của chúng tôi ngay bây giờ

Đang tải ý chính 9213f603f3807c030ea4c9081a27ee16

Khi bạn tạo một bài đăng trò chơi, bạn sẽ thấy bộ chọn mẫu có hai tùy chọn. Đánh giá mặc định và trò chơi. Sau khi bạn chọn mẫu cho bài đăng, mã trong mẫu sẽ được sử dụng cho bài đăng đó

Plugin mẫu bài đăng tùy chỉnh WordPress
Đây là bài đánh giá trò chơi mặc định của tôi

Plugin mẫu bài đăng tùy chỉnh WordPress
Và một hình ảnh khác từ bài đánh giá trò chơi của tôi

Để tạo các chế độ xem, tôi chủ yếu sao chép từ Twenty Seventeen. Tôi đã tạo tệp

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
5 với đoạn mã sau

Đang tải ý chính 535174e290dbbdc950b4cb6d53d633a6

Tôi có tất cả những thứ đó từ

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
6 trong Twenty Seventeen. Tôi đã sửa đổi hàm
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
7 bằng một tham số để kéo vào một tiêu đề khác (để đảm bảo tiêu đề của trang web đã bị xóa). Tôi cũng đã sửa đổi các tham số của hàm
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
8 để biến nó thành
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
9, nhưng chỉ vậy thôi

Sau đó, tôi đã tạo tệp tiêu đề mới header-review. php. Tôi đã sao chép mã từ tiêu đề của Twenty Seventeen. php vào đó và sau đó xóa dòng sau

Đang tải ý chính 690eafbc9ce99da77ed91ac59080de5d

Tôi đã theo dõi điều này bằng cách tạo các phần mẫu/trò chơi/đánh giá nội dung. php. Tôi đã dán nội dung của Twenty Seventeen. php vào đó và xóa một số mã không cần thiết vì nó chỉ liên quan đến các mục không phải trang đơn. Tôi đã thêm đoạn mã sau vào đầu trang

Đang tải ý chính d7240927b6bf0fc89c25de74fbba3b92

Tôi đã thêm một chút CSS để đặt nó lên trên cùng và tạo cho nó một số kiểu dáng cơ bản

Đang tải ý chính ac9b1cd88798ac2454f293994fb6c07d

kết thúc

Sử dụng các mẫu cực kỳ dễ dàng và cung cấp nhiều tùy chọn mới cho các nhà phát triển. Nó có thể làm cho mã của chúng tôi được tổ chức và chuẩn hóa tốt hơn trên các chủ đề khác nhau

Nếu bạn muốn xem tất cả hoạt động, hãy tải xuống ví dụ nhanh mà tôi đã thực hiện để giới thiệu cải tiến này

Nếu bạn có một số ý tưởng về cách chúng tôi có thể sử dụng chức năng mới này, hãy cho chúng tôi biết trong các nhận xét bên dưới

Các loại bài đăng tùy chỉnh cho phép bạn vượt ra ngoài các bài đăng và trang và tạo các loại nội dung khác nhau cho trang web của bạn. Họ chuyển đổi trang web WordPress của bạn từ một nền tảng viết blog thành một hệ thống quản lý nội dung mạnh mẽ (CMS)

Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách dễ dàng tạo các loại bài đăng tùy chỉnh trong WordPress

Plugin mẫu bài đăng tùy chỉnh WordPress

Loại bài đăng tùy chỉnh trong WordPress là gì?

Trên trang web WordPress của bạn, các loại bài đăng được sử dụng để giúp phân biệt giữa các loại nội dung khác nhau trong WordPress. Bài đăng và trang đều là loại bài đăng nhưng được tạo để phục vụ các mục đích khác nhau

WordPress đi kèm với một vài loại bài đăng khác nhau theo mặc định

  • Bưu kiện
  • Trang
  • Tập tin đính kèm
  • Ôn tập
  • Menu điều hướng

Bạn cũng có thể tạo các loại bài đăng của riêng mình, được gọi là các loại bài đăng tùy chỉnh. Chúng hữu ích khi tạo nội dung có định dạng khác với bài đăng hoặc trang tiêu chuẩn

Chẳng hạn, nếu bạn điều hành một trang web đánh giá phim, thì có lẽ bạn sẽ muốn tạo một loại bài đăng đánh giá phim. Bạn cũng có thể tạo các loại bài đăng tùy chỉnh cho danh mục đầu tư, lời chứng thực và sản phẩm

Trên WPBeginner, chúng tôi sử dụng các loại bài đăng tùy chỉnh cho các phần Thỏa thuận và Bảng thuật ngữ để tách chúng khỏi các bài viết blog hàng ngày của chúng tôi. Nó giúp chúng tôi sắp xếp nội dung trang web tốt hơn

Các loại bài đăng tùy chỉnh có thể có các trường tùy chỉnh khác nhau và cấu trúc danh mục tùy chỉnh của riêng chúng

Nhiều plugin WordPress phổ biến sử dụng các loại bài đăng tùy chỉnh để lưu trữ dữ liệu trên trang web WordPress của bạn. Sau đây là một vài plugin hàng đầu sử dụng các loại bài đăng tùy chỉnh

  • WooC Commerce thêm loại bài đăng 'sản phẩm' vào cửa hàng trực tuyến của bạn
  • WPForms tạo loại bài đăng 'wpforms' để lưu trữ tất cả các biểu mẫu của bạn
  • MemberPress thêm loại bài đăng tùy chỉnh 'sản phẩm thành viên'

Video hướng dẫn

Đăng ký WPBeginner

Nếu bạn muốn hướng dẫn bằng văn bản, hãy tiếp tục đọc

Tôi có cần tạo các loại bài đăng tùy chỉnh không?

Trước khi bạn bắt đầu tạo các loại bài đăng tùy chỉnh trên trang web WordPress của mình, điều quan trọng là phải đánh giá nhu cầu của bạn. Rất nhiều lần bạn có thể đạt được kết quả tương tự với một bài đăng hoặc trang bình thường

Nếu bạn không chắc trang web của mình có cần loại bài đăng tùy chỉnh hay không, hãy tham khảo hướng dẫn của chúng tôi về thời điểm bạn cần loại bài đăng tùy chỉnh hoặc phân loại trong WordPress

Như đã nói, chúng ta hãy xem cách dễ dàng tạo các loại bài đăng tùy chỉnh trong WordPress để bạn sử dụng

Chúng tôi sẽ chỉ cho bạn hai phương pháp và cũng đề cập đến một số cách bạn có thể hiển thị các loại bài đăng tùy chỉnh trên trang web WordPress của mình

Tạo loại bài đăng tùy chỉnh theo cách thủ công bằng WPCode

Tạo một loại bài đăng tùy chỉnh yêu cầu bạn thêm mã vào các chức năng của chủ đề của bạn. tập tin php. Thông thường, chúng tôi không khuyến nghị điều này cho bất kỳ ai ngoại trừ những người dùng nâng cao vì ngay cả một lỗi nhỏ cũng có thể làm hỏng trang web của bạn. Ngoài ra, nếu bạn cập nhật chủ đề của mình thì mã sẽ bị xóa

Tuy nhiên, chúng tôi sẽ sử dụng WPCode, cách dễ nhất và an toàn nhất để mọi người thêm mã tùy chỉnh vào trang web WordPress của bạn

Với WPCode, bạn có thể thêm các đoạn mã tùy chỉnh, cũng như kích hoạt nhiều tính năng từ thư viện mã được cấu hình sẵn, tích hợp sẵn có thể thay thế nhiều plugin chuyên dụng hoặc sử dụng một lần mà bạn có thể đã cài đặt

Trước tiên, bạn sẽ cần cài đặt và kích hoạt plugin WPCode miễn phí. Để biết hướng dẫn chi tiết, hãy xem hướng dẫn từng bước của chúng tôi về cách cài đặt plugin WordPress

Sau khi được kích hoạt, hãy điều hướng đến Đoạn mã »Thêm đoạn mã trong bảng điều khiển WordPress của bạn. Di chuột qua 'Thêm mã tùy chỉnh của bạn (đoạn mã mới)' rồi nhấp vào 'Sử dụng đoạn mã. ’

Plugin mẫu bài đăng tùy chỉnh WordPress

Tiếp theo, bạn sẽ được đưa đến màn hình ‘Tạo đoạn mã tùy chỉnh’

Bây giờ, bạn có thể đặt tiêu đề cho đoạn mã của mình và chuyển công tắc thành 'Đang hoạt động'. ’

Plugin mẫu bài đăng tùy chỉnh WordPress

Sau đó, chỉ cần dán đoạn mã sau vào khu vực 'Xem trước mã'. Mã này tạo một loại bài đăng tùy chỉnh cơ bản có tên là 'Phim' sẽ xuất hiện trong thanh bên quản trị viên của bạn và nó sẽ hoạt động với bất kỳ chủ đề nào

// Our custom post type function
function create_posttype() {
 
    register_post_type( 'movies',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Movies' ),
                'singular_name' => __( 'Movie' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'movies'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

Nếu bạn chỉ muốn một loại bài đăng tùy chỉnh cơ bản, thì chỉ cần thay thế

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
0 và
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
1 bằng sên và tên CPT của riêng bạn và nhấp vào nút 'Cập nhật'

Tuy nhiên, nếu bạn muốn có nhiều tùy chọn hơn cho loại bài đăng tùy chỉnh của mình, hãy sử dụng mã sau thay vì mã ở trên

Mã bên dưới thêm nhiều tùy chọn khác cho loại bài đăng tùy chỉnh 'Phim', chẳng hạn như hỗ trợ sửa đổi, hình ảnh nổi bật, trường tùy chỉnh, cũng như liên kết loại bài đăng tùy chỉnh với phân loại tùy chỉnh được gọi là 'thể loại. ’

Ghi chú. Không kết hợp hai đoạn mã này hoặc WordPress sẽ báo lỗi cho bạn vì cả hai đoạn mã đăng ký cùng một loại bài đăng tùy chỉnh. Chúng tôi khuyên bạn nên tạo một đoạn mã hoàn toàn mới bằng WPCode cho từng loại bài đăng bổ sung mà bạn muốn đăng ký

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );

Bạn cũng có thể nhận thấy phần mà chúng tôi đã đặt giá trị phân cấp thành

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
2. Nếu bạn muốn loại bài đăng tùy chỉnh của mình hoạt động giống như Trang hơn là Bài đăng, thì bạn có thể đặt giá trị này thành
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
3

Một điều khác cần lưu ý là việc sử dụng lặp đi lặp lại chuỗi

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
0, đây được gọi là Miền văn bản. Nếu chủ đề của bạn đã sẵn sàng dịch và bạn muốn các loại bài đăng tùy chỉnh của mình được dịch, thì bạn cần đề cập đến miền văn bản mà chủ đề của bạn sử dụng

Bạn có thể tìm thấy miền văn bản của chủ đề bên trong tệp

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
1 trong thư mục chủ đề của mình hoặc bằng cách đi tới Giao diện » Trình chỉnh sửa tệp chủ đề trong bảng quản trị của bạn. Miền văn bản sẽ được đề cập trong tiêu đề của tệp

Plugin mẫu bài đăng tùy chỉnh WordPress

Chỉ cần thay thế

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
0 bằng Tên miền văn bản chủ đề của riêng bạn

Khi bạn hài lòng với những thay đổi, chỉ cần nhấp vào nút 'Cập nhật' và WPCode sẽ xử lý phần còn lại

Tạo một loại bài đăng tùy chỉnh với một plugin

Một cách dễ dàng khác để tạo loại bài đăng tùy chỉnh trong WordPress là sử dụng plugin. Phương pháp này được khuyên dùng cho người mới bắt đầu vì nó an toàn và cực kỳ dễ dàng

Điều đầu tiên bạn cần làm là cài đặt và kích hoạt plugin Custom Post Type UI. Để biết thêm chi tiết, hãy xem hướng dẫn từng bước của chúng tôi về cách cài đặt plugin WordPress

Sau khi kích hoạt, bạn cần vào Giao diện người dùng CPT » Thêm / Chỉnh sửa loại bài viết để tạo loại bài đăng tùy chỉnh mới. Bạn nên ở trên tab 'Thêm loại bài đăng mới'

Plugin mẫu bài đăng tùy chỉnh WordPress

Trước tiên, bạn cần cung cấp slug cho loại bài đăng tùy chỉnh của mình, chẳng hạn như 'phim'. Con sên này sẽ được sử dụng trong URL và trong các truy vấn của WordPress, vì vậy nó chỉ có thể chứa các chữ cái và số. Bên dưới đó, bạn cần cung cấp tên số nhiều và số ít cho loại bài đăng tùy chỉnh của mình

Sau đó, nếu muốn, bạn có thể nhấp vào liên kết có nội dung 'Điền nhãn bổ sung dựa trên nhãn đã chọn'. Thao tác này sẽ tự động điền vào các trường nhãn bổ sung bên dưới và thường sẽ giúp bạn tiết kiệm thời gian

Bây giờ bạn có thể cuộn xuống phần 'Nhãn bổ sung' đó. Nếu bạn không nhấp vào liên kết mà chúng tôi đã đề cập, bây giờ bạn sẽ cần cung cấp mô tả cho loại bài đăng của mình và thay đổi nhãn

Plugin mẫu bài đăng tùy chỉnh WordPress

Các nhãn này sẽ được sử dụng trong toàn bộ giao diện người dùng WordPress khi bạn đang quản lý nội dung trong loại bài đăng cụ thể đó

Tiếp theo là cài đặt loại bài đăng. Từ đây, bạn có thể thiết lập các thuộc tính khác nhau cho loại bài đăng của mình. Mỗi tùy chọn đi kèm với một mô tả ngắn gọn giải thích những gì nó làm

Plugin mẫu bài đăng tùy chỉnh WordPress

Chẳng hạn, bạn có thể chọn không tạo một loại bài đăng theo thứ bậc như các trang hoặc sắp xếp các bài đăng theo trình tự thời gian đảo ngược

Bên dưới cài đặt chung, bạn sẽ thấy tùy chọn để chọn các tính năng chỉnh sửa mà loại bài đăng này sẽ hỗ trợ. Chỉ cần kiểm tra các tùy chọn mà bạn muốn được đưa vào

Plugin mẫu bài đăng tùy chỉnh WordPress

Cuối cùng, nhấp vào nút ‘Thêm loại bài đăng’ để lưu và tạo loại bài đăng tùy chỉnh của bạn

Đó là tất cả. Bạn đã tạo thành công loại bài đăng tùy chỉnh của mình và có thể tiếp tục và bắt đầu thêm nội dung

Hiển thị các loại bài đăng tùy chỉnh trên trang web của bạn

WordPress đi kèm với hỗ trợ tích hợp để hiển thị các loại bài đăng tùy chỉnh của bạn. Khi bạn đã thêm một vài mục vào loại bài đăng tùy chỉnh mới của mình, đã đến lúc hiển thị chúng trên trang web của bạn

Có một vài phương pháp mà bạn có thể sử dụng và mỗi phương pháp đều có những lợi ích riêng

Hiển thị các loại bài đăng tùy chỉnh bằng mẫu lưu trữ mặc định

Trước tiên, bạn chỉ cần vào Giao diện »Menu và thêm liên kết tùy chỉnh vào menu của mình. Liên kết tùy chỉnh này là liên kết đến loại bài đăng tùy chỉnh của bạn

Plugin mẫu bài đăng tùy chỉnh WordPress

Nếu bạn đang sử dụng permalinks thân thiện với SEO, thì URL loại bài đăng tùy chỉnh của bạn rất có thể sẽ giống như thế này

http://example.com/movies

Nếu bạn không sử dụng permalinks thân thiện với SEO, thì URL loại bài đăng tùy chỉnh của bạn sẽ giống như thế này

http://example.com/?post_type=movies

Đừng quên thay thế 'ví dụ. com' với tên miền của riêng bạn và 'phim' với tên loại bài đăng tùy chỉnh của bạn

Lưu menu của bạn và sau đó truy cập giao diện người dùng của trang web của bạn. Bạn sẽ thấy mục menu mới mà bạn đã thêm và khi bạn nhấp vào nó, nó sẽ hiển thị trang lưu trữ loại bài đăng tùy chỉnh của bạn bằng cách sử dụng kho lưu trữ. tệp mẫu php trong chủ đề của bạn

Plugin mẫu bài đăng tùy chỉnh WordPress

Tạo mẫu loại bài đăng tùy chỉnh

Nếu bạn không thích giao diện của trang lưu trữ cho loại bài đăng tùy chỉnh của mình, thì bạn có thể sử dụng mẫu dành riêng cho kho lưu trữ loại bài đăng tùy chỉnh

Tất cả những gì bạn cần làm là tạo một tệp mới trong thư mục chủ đề của mình và đặt tên là

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
3. Đảm bảo rằng bạn thay thế 'phim' bằng tên của loại bài đăng tùy chỉnh của mình

Để bắt đầu, bạn có thể sao chép nội dung trong tệp

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
4 của chủ đề vào mẫu
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
3 rồi sửa đổi nó để đáp ứng nhu cầu của bạn

Bây giờ, bất cứ khi nào trang lưu trữ cho loại bài đăng tùy chỉnh của bạn được truy cập, mẫu này sẽ được sử dụng để hiển thị nó

Tương tự, bạn cũng có thể tạo một mẫu tùy chỉnh để hiển thị mục đơn của loại bài đăng của mình. Để làm điều đó, bạn cần tạo

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
6 trong thư mục chủ đề của mình. Đừng quên thay thế 'phim' bằng tên của loại bài đăng tùy chỉnh của bạn

Bạn có thể bắt đầu bằng cách sao chép nội dung của mẫu

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
7 trong chủ đề của mình vào mẫu
/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
6 và sau đó bắt đầu sửa đổi nó để đáp ứng nhu cầu của bạn

Để tìm hiểu thêm, hãy xem hướng dẫn của chúng tôi về cách tạo các mẫu bài đăng đơn tùy chỉnh trong WordPress

Hiển thị các loại bài đăng tùy chỉnh trên trang chủ

Một lợi thế của việc sử dụng các loại bài đăng tùy chỉnh là nó giữ cho các loại nội dung tùy chỉnh của bạn tách biệt với các bài đăng thông thường của bạn. Tuy nhiên, nếu muốn, bạn có thể hiển thị các loại bài đăng tùy chỉnh trên trang nhất của trang web của mình

Chỉ cần thêm mã này dưới dạng đoạn mã mới bằng cách sử dụng plugin WPCode miễn phí. Vui lòng xem hướng dẫn chi tiết

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
3

Đừng quên thay thế 'phim' bằng loại bài đăng tùy chỉnh của bạn

Truy vấn các loại bài đăng tùy chỉnh

Nếu bạn đã quen viết mã và muốn chạy các truy vấn vòng lặp trong các mẫu của mình, thì đây là cách để thực hiện điều đó. Bằng cách truy vấn cơ sở dữ liệu, bạn có thể truy xuất các mục từ loại bài đăng tùy chỉnh

Bạn sẽ cần sao chép đoạn mã sau vào mẫu mà bạn muốn hiển thị loại bài đăng tùy chỉnh

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );
4

Mã này xác định loại bài đăng và số lượng bài đăng trên mỗi trang trong các đối số cho lớp WP_Query mới của chúng tôi. Sau đó, nó sẽ chạy truy vấn, truy xuất các bài đăng và hiển thị chúng bên trong vòng lặp

Hiển thị các loại bài đăng tùy chỉnh trong Widget

Bạn sẽ nhận thấy rằng có một tiện ích mặc định trong WordPress để hiển thị các bài đăng gần đây, nhưng nó không cho phép bạn chọn loại bài đăng tùy chỉnh

Điều gì sẽ xảy ra nếu bạn muốn hiển thị các mục mới nhất từ ​​loại bài đăng mới tạo của mình trong một tiện ích?

Điều đầu tiên bạn cần làm là cài đặt và kích hoạt plugin Custom Post Type Widgets. Để biết thêm chi tiết, hãy xem hướng dẫn từng bước của chúng tôi về cách cài đặt plugin WordPress

Sau khi kích hoạt, chỉ cần vào Giao diện » Tiện ích và kéo và thả tiện ích 'Bài đăng gần đây (Loại bài đăng tùy chỉnh)' vào thanh bên

Plugin mẫu bài đăng tùy chỉnh WordPress

Tiện ích này cho phép bạn hiển thị các bài đăng gần đây từ bất kỳ loại bài đăng nào. Bạn cần chọn loại bài đăng tùy chỉnh của mình từ menu thả xuống 'Loại bài đăng' và chọn các tùy chọn bạn muốn

Sau đó, đảm bảo bạn nhấp vào nút 'Cập nhật' ở đầu màn hình rồi truy cập trang web của bạn để xem tiện ích hoạt động

Plugin mẫu bài đăng tùy chỉnh WordPress

Plugin cũng cung cấp các tiện ích loại bài đăng tùy chỉnh hiển thị lưu trữ, lịch, danh mục, nhận xét gần đây, tìm kiếm và đám mây thẻ

Plugin mẫu bài đăng tùy chỉnh WordPress

Chúng tôi hy vọng hướng dẫn này đã giúp bạn tìm hiểu cách tạo các loại bài đăng tùy chỉnh trong WordPress. Bạn cũng có thể muốn tìm hiểu cách tăng lưu lượng truy cập blog của mình hoặc xem danh sách các lỗi WordPress phổ biến của chúng tôi và cách khắc phục chúng

Nếu bạn thích bài viết này, vui lòng đăng ký Kênh YouTube của chúng tôi để xem các video hướng dẫn về WordPress. Bạn cũng có thể tìm thấy chúng tôi trên Twitter và Facebook

Làm cách nào để tạo mẫu tùy chỉnh cho loại bài đăng tùy chỉnh trong WordPress?

Loại bài đăng tùy chỉnh – Phân cấp mẫu . Vì vậy, nếu bạn muốn tạo mẫu tùy chỉnh cho loại bài đăng tùy chỉnh acme_product của mình, bạn nên bắt đầu bằng cách sao chép mẫu đơn. tệp php, lưu nó dưới dạng single-acme_product.

Làm cách nào để tạo loại bài đăng tùy chỉnh trong WordPress mà không cần plugin?

Tất cả những gì bạn cần làm là tạo một tệp mới trong thư mục chủ đề của bạn và đặt tên là archive-phim. php . Đảm bảo rằng bạn thay thế 'phim' bằng tên của loại bài đăng tùy chỉnh của mình. Để bắt đầu, bạn có thể sao chép nội dung trong kho lưu trữ của chủ đề.

Giao diện người dùng CPT có miễn phí không?

Nhà phát triển. WebDevStudio. Đặc trưng. Giao diện người dùng loại bài đăng tùy chỉnh là plugin xây dựng phân loại và CPT miễn phí đầy đủ tính năng với giao diện bảng điều khiển đơn giản và trực quan.

Plugin CPT là gì?

CPT là viết tắt của Loại bài đăng tùy chỉnh . WordPress sử dụng các bài đăng như một cách lưu trữ các phần nội dung khác nhau. Nội dung này sau đó có thể được sử dụng theo nhiều cách khác nhau. Mặc dù chúng được gọi là bài đăng, nhưng chúng có thể được sử dụng cho bất kỳ mục đích nào yêu cầu lưu trữ, thay đổi và xóa nội dung.