Nhận tất cả các loại bài đăng WordPress

Gần đây, tôi gặp một tình huống cần xác định xem loại bài đăng có phải là một trong các loại cốt lõi của WordPress hay không. WordPress có một số loại bài đăng cơ sở

  • Bài đăng (Loại bài đăng. 'bưu kiện')
  • Trang (Loại bài đăng. 'trang')
  • Tệp đính kèm (Loại bài đăng. 'tập tin đính kèm')
  • Sửa đổi (Loại bài. 'ôn tập')
  • Menu điều hướng (Loại bài đăng. ‘nav_menu_item’)

Ví dụ: giả sử rằng chúng tôi cần hiển thị một trường được chọn trong phần phụ trợ với tất cả các loại bài đăng hiện đã được đăng ký trong WordPress. Chúng tôi đang lên kế hoạch tạo một plugin và chúng tôi muốn người dùng có thể chọn một loại bài đăng cụ thể. Vì vậy, chúng tôi cần tự động lấy các loại bài đăng đã đăng ký trong trường hợp họ có các loại bài đăng tùy chỉnh bổ sung của riêng họ hoặc từ một chủ đề hoặc plugin

WordPress có một chức năng cốt lõi cho việc này ( get_post_types() ), theo mặc định, sẽ trả về một mảng tên hoặc đối tượng loại bài đăng

Những gì bạn có thể thấy là Bài đăng, Trang và Tệp đính kèm từ đầu ra của chức năng này. Nhưng khi bạn bắt đầu hiển thị trường, bạn cũng nhận được menu Điều hướng và Sửa đổi. Đó không phải là những loại bài đăng mà chúng tôi mong đợi người dùng cần, vì vậy sẽ là một ý tưởng hay nếu bạn lọc chúng ra

Hàm get_post_types() lấy một mảng đối số làm tham số đầu tiên mà chúng ta có thể cung cấp với 'public' => true và nó sẽ chỉ bao gồm các loại bài đăng đã đăng ký đã được xác định là 'public'. Chúng tôi cũng có thể chuyển 'đối tượng' làm tham số thứ hai để đảm bảo rằng chúng tôi lấy lại toàn bộ đối tượng loại bài đăng thay vì chỉ tên loại bài đăng. Sử dụng các nhãn khác nhau sẽ hữu ích khi cấu trúc trường

Các loại bài đăng sửa đổi và nav_menu_item đều được đăng ký ở chế độ riêng tư nên không có chúng được liệt kê, chúng tôi có thể kết xuất trường của mình

Xin chào, tôi là Daniel và tôi làm mọi thứ cho web. Tôi là CTO tại Kinsta và tôi viết cho một số ấn phẩm tuyệt vời như Tạp chí Smashing và … Thông tin thêm về Daniel ↬

Bản tin email

Email (đập vỡ) của bạn

Mẹo hàng tuần về giao diện người dùng & UX.
Được hơn 200.000 người tin cậy.

  • Nhận tất cả các loại bài đăng WordPress
    Giao diện người dùng SmashingConf 2023

  • Nhận tất cả các loại bài đăng WordPress
    Bắt đầu miễn phí
  • Nhận tất cả các loại bài đăng WordPress
    Lớp học tổng thể về nguyên tắc phổ quát của kiểu chữ với Elliot Jay Stocks

  • Nhận tất cả các loại bài đăng WordPress
    Hệ thống thiết kế thành công

  • Nhận tất cả các loại bài đăng WordPress
    Các mẫu thiết kế giao diện thông minh, khóa học 9h-video

WordPress đã có chỗ đứng trong trò chơi CMS nói chung được vài năm nhưng bước đột phá thực sự là cơ chế loại bài đăng tùy chỉnh cho phép tạo ra nhiều loại nội dung. Chúng ta hãy xem điều này xảy ra như thế nào và tất cả các tùy chọn mà chức năng tuyệt vời này cung cấp

WordPress đã đạt được chỗ đứng trong trò chơi hệ thống quản lý nội dung chung (CMS) được vài năm nay, nhưng bước đột phá thực sự là cơ chế loại bài đăng tùy chỉnh cho phép tạo ra nhiều loại nội dung. Chúng ta hãy xem điều này xảy ra như thế nào và tất cả các tùy chọn mà chức năng tuyệt vời này cung cấp

Nhận tất cả các loại bài đăng WordPress

Một số loại bài đăng tùy chỉnh bạn có thể tạo trong WordPress

Nó đã từng như thế nào

Trên thực tế, các loại bài đăng tùy chỉnh đã xuất hiện từ lâu, cụ thể hơn là kể từ ngày 17 tháng 2 năm 2005, khi WordPress 1. 5 đã thêm hỗ trợ cho các trang tĩnh, tạo trường cơ sở dữ liệu

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1

Đọc thêm trên SmashingMag.

  • Mở rộng WordPress với các loại nội dung tùy chỉnh
  • Hướng dẫn chi tiết về các mẫu trang tùy chỉnh WordPress
  • Tùy chỉnh kho lưu trữ WordPress cho danh mục, thẻ
  • Xây dựng trang lưu trữ tùy chỉnh cho WordPress

Thêm sau khi nhảy. Tiếp tục đọc bên dưới ↓

Gặp gỡ Thiết kế Cảm ứng cho Giao diện Di động, hướng dẫn hoàn toàn mới của Steven Hoober về thiết kế cho thiết bị di động với các nguyên tắc lấy con người làm trung tâm, đã được kiểm chứng. 400 trang, chứa đầy nghiên cứu chuyên sâu về người dùng và các phương pháp hay nhất

Chuyển đến mục lục ↬

Nhận tất cả các loại bài đăng WordPress

Hàm

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
2 đã có từ WordPress 1. 0, vì vậy khi trường
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1 được triển khai trong 1. 5, bạn chỉ cần đặt giá trị
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1 khi chèn bài đăng. Tất nhiên, việc tạo và quản lý các loại bài đăng tùy chỉnh đòi hỏi nhiều hơn thế, nhưng số lượng mã hóa cần thiết ngày càng ít hơn khi các chức năng của WordPress ngày càng linh hoạt hơn

Theo phiên bản 2. 8, chức năng

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
5 và một số thứ hữu ích khác đã được thêm vào bản dựng hàng đêm và khi 2. 9 ra mắt, các chức năng đã có sẵn cho tất cả mọi người. Tại thời điểm này, không cần mã hóa và hack nhiều để biến WordPress thành một CMS hoàn chỉnh;

Những gì WordPress có thể làm cho bạn bây giờ

Loại bài đăng tùy chỉnh không gì khác hơn là một bài đăng thông thường có giá trị

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1 khác trong cơ sở dữ liệu. Loại bài đăng của bài đăng thông thường là
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
7, trang sử dụng
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
8, tệp đính kèm sử dụng
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
9, v.v. Giờ đây, bạn có thể tạo nội dung của riêng mình để cho biết loại nội dung được tạo. Bạn có thể tạo các loại bài đăng tùy chỉnh cho sách, phim, đánh giá, sản phẩm, v.v.

Nếu được tạo chính xác, bạn có thể đạt được những điều sau với một vài dòng mã

  • Loại bài đăng tùy chỉnh sẽ hiển thị ở mặt sau dưới dạng một mục menu riêng với danh sách bài đăng riêng và trang “thêm mới”
  • Điều hướng đến
    function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    0 sẽ đưa bạn đến trang lưu trữ cho loại bài đăng. Điều này giống như việc truy cập trang nhất để xem các bài đăng mới nhất từ ​​loại bài đăng “post”
  • Danh mục và thẻ có thể được cung cấp cho loại bài đăng tùy chỉnh hoặc bạn có thể tạo phân loại tùy chỉnh

Ngoài những điều này, bạn có thể sửa đổi vô số tùy chọn, chẳng hạn như vị trí đặt loại bài đăng tùy chỉnh trong menu, liệu nó có thể tìm kiếm được không, cấp độ người dùng nào có thể truy cập nó, nó có được phân cấp, quy tắc viết lại tùy chỉnh, v.v.

Các loại nội dung khác nhau có yêu cầu dữ liệu khác nhau. Đối với các bài đăng thông thường, bạn sẽ muốn chỉ định tác giả, danh mục, ngày tháng, v.v. Đối với loại bài đăng tùy chỉnh “sách”, lý tưởng nhất là bạn muốn có tùy chọn chỉ định tác giả của sách, số trang, thể loại, nhà xuất bản và các dữ liệu cụ thể khác về sách. Sử dụng các hộp meta tùy chỉnh, điều này cũng dễ dàng đạt được và quản lý

Hộp meta tùy chỉnh cho phép bạn thêm các hộp bổ sung vào màn hình chỉnh sửa của bài đăng. Họ thường sử dụng các trường tùy chỉnh, vì vậy bạn cũng có thể sử dụng các trường tùy chỉnh, nhưng bằng cách tách một số trường tùy chỉnh thành hộp meta, bạn có thể tạo quản trị viên dễ sử dụng và mượt mà hơn nhiều

Làm việc với các loại bài đăng tùy chỉnh

Để tạo và sử dụng các loại bài đăng tùy chỉnh một cách hiệu quả, bạn cần làm quen với những điều sau

  • Tạo các loại bài đăng tùy chỉnh,
  • Tạo phân loại tùy chỉnh,
  • Tạo hộp meta tùy chỉnh

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

Đầu tiên trong chương trình nghị sự của chúng tôi là tự tạo loại bài đăng. Tốt nhất là bạn nên tạo một plugin khi làm việc với các loại bài đăng tùy chỉnh, nhưng nếu bạn không biết cách hoặc chỉ cần thử nghiệm nhanh, bạn có thể sử dụng tệp

function my_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['product'] = array(
    0 => ’, 
    1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Product updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Product saved.'),
    8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages' );
1 trong chủ đề của mình

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1

Ở dạng đơn giản nhất, nó sẽ tạo một loại bài đăng gần như không có tùy chỉnh. Nó sẽ không công khai, nó sẽ không hiển thị trong quản trị viên, các thông báo tương tác sẽ giống như các bài đăng (“bài đăng đã lưu”, “bài đăng được cập nhật”, v.v. ) và như thế. Để điều chỉnh loại bài đăng mới của chúng tôi theo nhu cầu của chúng tôi, tôi sẽ xem xét một số tùy chọn được sử dụng thường xuyên hơn và thêm chúng vào mảng

function my_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['product'] = array(
    0 => ’, 
    1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Product updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Product saved.'),
    8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages' );
2 trống trước đó

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    3 Tùy chọn
    function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    3 phải là một mảng xác định các nhãn khác nhau mà loại bài đăng tùy chỉnh có thể có. Tôi đã tách phần này ra ở trên chỉ để làm cho các đối số đăng ký một loại bài viết rõ ràng hơn
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    5 Giải thích ngắn gọn về loại bài đăng tùy chỉnh của chúng tôi;
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    6 Tùy chọn này kiểm soát nhiều thứ cùng một lúc. Đặt điều này thành true sẽ đặt một loạt các tùy chọn khác (tất cả đều liên quan đến khả năng hiển thị) thành true. Ví dụ: có thể hiển thị loại bài đăng tùy chỉnh nhưng không thể truy vấn được. Thêm về điều này sau
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    7 Xác định vị trí của menu loại bài đăng tùy chỉnh ở phía sau. Đặt nó thành “5” sẽ đặt nó bên dưới menu “bài đăng”;
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    8 Tùy chọn này thiết lập các điều khiển WordPress mặc định có sẵn trong màn hình chỉnh sửa cho loại bài đăng tùy chỉnh. Theo mặc định, chỉ có trường tiêu đề và trình chỉnh sửa được hiển thị. Nếu bạn muốn thêm hỗ trợ cho nhận xét, sửa đổi, định dạng bài đăng, v.v., bạn sẽ cần chỉ định chúng tại đây. Để biết danh sách đầy đủ, hãy xem phần đối số trong Codex
  • function my_updated_messages( $messages ) {
      global $post, $post_ID;
      $messages['product'] = array(
        0 => ’, 
        1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Product updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
      return $messages;
    }
    add_filter( 'post_updated_messages', 'my_updated_messages' );
    9 Nếu được đặt thành true, các quy tắc viết lại sẽ được tạo cho bạn, cho phép lưu trữ loại bài đăng tại
    function my_custom_post_product() {
      $labels = array(
        'name'               => _x( 'Products', 'post type general name' ),
        'singular_name'      => _x( 'Product', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Product' ),
        'edit_item'          => __( 'Edit Product' ),
        'new_item'           => __( 'New Product' ),
        'all_items'          => __( 'All Products' ),
        'view_item'          => __( 'View Product' ),
        'search_items'       => __( 'Search Products' ),
        'not_found'          => __( 'No products found' ),
        'not_found_in_trash' => __( 'No products found in the Trash' ), 
        'parent_item_colon'  => ’,
        'menu_name'          => 'Products'
      );
      $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our products and product specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
      );
      register_post_type( 'product', $args ); 
    }
    add_action( 'init', 'my_custom_post_product' );
    10 (theo mặc định)
Nhận tất cả các loại bài đăng WordPress
Một loại bài đăng tùy chỉnh trong menu

Sau khi thiết lập điều này, bạn sẽ thấy mục menu cho loại bài đăng tùy chỉnh. Bạn sẽ có thể thêm bài đăng, xem danh sách bài đăng trong quản trị viên và cũng có thể truy cập các bài đăng đã xuất bản trên trang web

Như tôi đã đề cập, có rất nhiều thứ bạn có thể sửa đổi khi tạo loại bài đăng. Tôi khuyên bạn nên xem danh sách đối số trong Codex để biết mô tả đầy đủ về từng tùy chọn và các giá trị có thể

Tin nhắn tương tác tùy chỉnh

WordPress tạo ra một số thông báo được kích hoạt bởi hành động của người dùng. Cập nhật, xuất bản, tìm kiếm, v.v. , ở mặt sau, tất cả đều dẫn đến các thông báo — theo mặc định — được điều chỉnh cho phù hợp với các bài đăng thông thường. Bạn có thể thay đổi văn bản của những thông báo này một cách dễ dàng bằng cách sử dụng móc nối

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
11

function my_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['product'] = array(
    0 => ’, 
    1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Product updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Product saved.'),
    8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages' );
Nhận tất cả các loại bài đăng WordPress
Thông báo tùy chỉnh sau khi lưu sản phẩm.

Như bạn có thể thấy, đây không phải là phương pháp quản lý tin nhắn thân thiện với người dùng nhất. Một mảng kết hợp sẽ tốt hơn nhiều;

Lưu ý rằng bạn có thể thay đổi thông báo cho tất cả các loại bài đăng tùy chỉnh bằng chức năng duy nhất này. Mảng

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
12 chứa thông báo cho tất cả các loại bài đăng, vì vậy bạn có thể sửa đổi tất cả chúng tại đây. Cá nhân tôi tạo một chức năng cho từng loại bài đăng để tôi có thể nhóm việc tạo loại bài đăng và các thông báo tùy chỉnh lại với nhau một cách dễ dàng

Trợ giúp theo ngữ cảnh

Một tính năng tôi hiếm khi thấy được triển khai là trợ giúp theo ngữ cảnh tùy chỉnh. Là một người dùng, bản thân tôi chưa bao giờ thực sự sử dụng tính năng này, nhưng tôi chắc rằng nhiều người cũng vậy;

Tính năng trợ giúp theo ngữ cảnh là một tab giảm dần có thể nhìn thấy ở trên cùng bên phải của các trang nếu có. Hãy xem cách thay đổi nội dung

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1

Cái này cũng hơi khó vì bạn phải biết ID của màn hình bạn đang bật. Nếu bạn in ra nội dung của biến

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
13, bạn sẽ có thể dễ dàng xác định ID. Đây cũng là một chức năng bạn có thể sử dụng để sửa đổi trợ giúp theo ngữ cảnh của tất cả các loại bài đăng tùy chỉnh cùng một lúc, nhưng cá nhân tôi khuyên bạn nên nhóm chức năng này cùng với hai khối trước đó và chỉ sử dụng nó cho một loại bài đăng tùy chỉnh tại một thời điểm

Tổng quan

Để tóm tắt nhanh, chúng tôi đã sử dụng ba chức năng để tạo loại bài đăng tùy chỉnh “hoàn chỉnh”. Chúng tôi đã sử dụng

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
5 để tự tạo loại bài đăng và hai hook —
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
15 và
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
11 — để tạo hướng dẫn hữu ích và các thông điệp liên quan tương ứng

Phân loại tùy chỉnh

Các bài đăng trên blog thông thường của bạn sử dụng các danh mục và thẻ để tạo cấu trúc tổ chức. Tuy nhiên, cùng một tổ chức không nhất thiết phải có ý nghĩa đối với các loại bài đăng tùy chỉnh. Các bài đăng trên blog của bạn có thể là về “Cuộc sống”, “Suy nghĩ” hoặc “Ước mơ” của bạn. ” Đây rõ ràng là không thích hợp cho các sản phẩm

Đây là vấn đề khiến các nhà phát triển tạo ra các nguyên tắc phân loại tùy chỉnh. Bạn có thể tạo một phân loại riêng có tên là “Danh mục sản phẩm” để chứa các danh mục mà bạn chỉ sử dụng cho các sản phẩm. Kevin Leary đã viết một bài viết tuyệt vời về phân loại tùy chỉnh trong WordPress mà tôi rất khuyến khích, vì vậy tôi sẽ chỉ đi vào chi tiết nhỏ ở đây

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
0

Tương tự như các loại bài đăng tùy chỉnh, bạn có thể tạo phân loại rất dễ dàng, nhưng bạn cần phải làm việc với nó một chút để điều chỉnh nó theo nhu cầu của mình. Các nguyên tắc phân loại tùy chỉnh hoạt động tốt hơn một chút vì chúng được công khai theo mặc định, vì vậy những điều trên thực sự đủ để liên kết nguyên tắc phân loại này với các bài đăng sản phẩm. Hãy xem một ví dụ tùy chỉnh

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
1

Như bạn có thể thấy, không có nhiều thay đổi. Chúng tôi đã thêm một số nhãn và đặt tùy chọn phân cấp thành true. Điều này cho phép phân loại "kiểu danh mục". Khi được đặt thành false (đây là mặc định), phân loại của bạn sẽ giống như các thẻ mặc định

Có một số tùy chọn nguồn điện khác mà bạn có thể đọc trong bài viết của Leary hoặc bạn có thể truy cập mục nhập Codex trên

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
17

Nhận tất cả các loại bài đăng WordPress
Danh mục sản phẩm phân loại tùy chỉnh

Đăng Hộp Meta

Hộp meta là hộp có thể kéo mà bạn thấy trong màn hình chỉnh sửa WordPress cho bài đăng. Có rất nhiều hộp meta tích hợp như điều khiển xuất bản, phân loại, hộp tác giả, v.v. , nhưng bạn có thể tạo một số cho chính mình

Các hộp meta có xu hướng được sử dụng để quản lý dữ liệu trường tùy chỉnh theo cách thân thiện với người dùng hơn nhiều so với hộp trường tùy chỉnh tích hợp sẵn. Vì bạn đặt các điều khiển tại chỗ, bạn có thể thêm kiểm tra lỗi phía máy khách và nhiều thứ thú vị khác

Justin Tadlock đã viết bài viết về hộp meta tùy chỉnh bao gồm tất cả tại đây trên Tạp chí Smashing, đây là một bài viết chuyên sâu tuyệt vời về chủ đề này. Tôi khuyên bạn nên đọc nó để có bức tranh đầy đủ, nhưng tôi sẽ giúp bạn bắt đầu tại đây

Tạo một hộp meta yêu cầu ba bước

  • Xác định hộp chính nó,
  • Xác định nội dung của hộp meta,
  • Xác định cách dữ liệu từ hộp được xử lý

Xác định Hộp Meta

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
3

Đoạn mã trên tạo hộp meta với các tham số sau (theo thứ tự đã cho)

  • Mã định danh duy nhất cho hộp meta (không nhất thiết phải khớp với tên hàm),
  • Tiêu đề của hộp meta (hiển thị cho người dùng),
  • Chức năng sẽ hiển thị nội dung của hộp,
  • Loại bài đăng mà hộp meta thuộc về,
  • Vị trí của hộp meta,
  • Mức độ ưu tiên của hộp meta (xác định mức độ cao của nó được đặt)

Xác định nội dung của Meta Box

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
4

Đây là một hộp đơn giản chỉ chứa giá, vì vậy chúng tôi đã tạo nhãn và đầu vào để quản lý nó. Một trường nonce cũng có mặt, giúp tăng thêm tính bảo mật cho việc gửi dữ liệu

Xử lý dữ liệu đã gửi

Trong hầu hết các trường hợp, bạn sẽ muốn lưu dữ liệu dưới dạng trường tùy chỉnh, nhưng bạn hoàn toàn không bị hạn chế đối với phương pháp này. Bạn có thể sử dụng đầu vào để thực hiện lệnh gọi API của bên thứ ba, để tạo tệp XML hoặc bất kỳ thứ gì bạn thích. Cách sử dụng phổ biến nhất là lưu dữ liệu bài đăng tùy chỉnh, vì vậy hãy xem cách thực hiện việc đó

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
5

Phần lớn nhất của chức năng này là tất cả về an toàn. Trước hết, nếu tự động lưu đang được thực hiện, sẽ không có gì xảy ra vì người dùng chưa thực sự gửi biểu mẫu. Sau đó, nonce được kiểm tra, tiếp theo là kiểm tra quyền. Nếu tất cả những điều này đều được thông qua, chúng tôi sẽ lấy dữ liệu của mình và thêm nó vào bài đăng bằng hàm

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
18

Nhận tất cả các loại bài đăng WordPress
Một ví dụ về hộp meta cho loại bài đăng căn hộ

Hiển thị nội dung của bạn

Mặc dù có rất nhiều sắc thái đối với tất cả những điều trên, nhưng bạn nên làm quen với những điều cơ bản. Tất cả những gì còn lại là thực sự sử dụng dữ liệu chúng tôi hiện có và hiển thị mọi thứ cho người dùng. Điều này liên quan đến việc hiển thị các bài đăng — có lẽ từ các loại bài đăng và phân loại tùy chỉnh khác nhau — và sử dụng siêu dữ liệu bài đăng của chúng tôi

Hiển thị bài viết

Nếu bạn đã tạo một loại bài đăng với tham số

function my_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['product'] = array(
    0 => ’, 
    1 => sprintf( __('Product updated. View product'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Product updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Product published. View product'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Product saved.'),
    8 => sprintf( __('Product submitted. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Product scheduled for: %1$s. Preview product'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Product draft updated. Preview product'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages' );
9 được đặt thành “true”, thì WordPress sẽ liệt kê các bài đăng của bạn trên trang lưu trữ của loại bài đăng đó. Nếu loại bài đăng của bạn được gọi là “sách”, bạn chỉ cần truy cập
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
00 và bạn sẽ thấy danh sách bài đăng của mình

Trang này sử dụng

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
01 để hiển thị nếu nó tồn tại (
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
02 trong trường hợp của chúng tôi). Nếu nó không tồn tại, nó sẽ sử dụng
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
03 và nếu nó không tồn tại, nó sẽ sử dụng
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
04

Một cách khác để hiển thị nội dung loại bài đăng tùy chỉnh là sử dụng truy vấn tùy chỉnh với lớp

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
05. Để hiển thị các bài đăng từ một loại bài đăng nhất định và phân loại tùy chỉnh, bạn có thể làm như thế này

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
4

Hiển thị siêu dữ liệu

Siêu dữ liệu có thể được truy xuất dễ dàng bằng hàm

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
06. Trong ví dụ của chúng tôi ở trên, chúng tôi đã lưu một trường meta bài đăng có tên
function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
07. Chúng tôi có thể truy xuất giá trị của trường này cho một bài đăng nhất định bằng mã sau

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );
0

Từ cuối cùng

Như bạn có thể thấy, việc tạo một CMS khá dễ dàng do tính chất mô-đun của các chức năng và điều khiển WordPress. Các phương pháp được nêu ở đây có thể được sử dụng cực kỳ hiệu quả để tạo quản trị viên tùy chỉnh cho hầu hết mọi thứ bạn có thể nghĩ đến

Vì việc xác định nội dung của các hộp meta hoàn toàn tùy thuộc vào bạn, nên bạn có quyền tạo các tính năng bổ sung cho các điều khiển của mình, khiến bạn hoặc khách hàng của bạn rất hài lòng

Bạn có thể tiến thêm một bước này với các trang quản trị tùy chỉnh và nội dung hoàn toàn tùy chỉnh, nhưng đó là câu chuyện của một ngày khác