WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình

Phần lớn thời gian tôi làm việc trong một dự án hợp đồng, cần phải lập trình một số phần của trang web xảy ra trong quá trình thiết lập chủ đề. Thông thường, điều này bao gồm tạo bài đăng, người dùng và/hoặc thiết lập mẫu

Cuối cùng, mục tiêu là làm cho trải nghiệm của người dùng tốt nhất có thể. Họ cài đặt chủ đề và trang web, ở một mức độ nào đó, đã tự khởi động

Như với các phần đã nói ở trên của một trang web, bạn cũng thường cần tạo một danh mục hoặc các danh mục trong quá trình thiết lập chủ đề. WordPress cung cấp hai cách để làm như vậy, một trong số đó thường dẫn đến lỗi nghiêm trọng PHP

Đây là cách lập trình tạo các danh mục trong WordPress và làm như vậy mà không tạo ra bất kỳ lỗi nào

Khi chèn danh mục

Tìm kiếm nhanh trên Google có thể sẽ dẫn bạn đến bài viết Codex cho

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. Thoạt nhìn, đây có vẻ chính xác là thứ cần thiết để tạo danh mục theo chương trình

Sử dụng chức năng API cụ thể này trông giống như thế này

function example_insert_category() {
	wp_insert_category(
		array(
		  'cat_name' 				=> 'Example Category',
		  'category_description'	=> 'This is an example category created with wp_insert_category.',
		  'category_nicename' 		=> 'example-category',
		  'taxonomy' 				=> 'category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );

Đủ rõ ràng, phải không?

Nếu bạn xác định chức năng này trong chủ đề của mình (hoặc ở đâu đó trong plugin), sau đó tải nó lên trình duyệt, bạn có thể thấy màn hình sau

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình

Ối

Rõ ràng, đó không phải là những gì chúng tôi muốn. Moar Chasen đã đưa ra một lời giải thích tốt về lý do tại sao điều này xảy ra trong

Điều đó nói rằng, có một chức năng thứ hai mà WordPress cung 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' );
1

Chức năng cụ thể này không dẫn đến lỗi đã nói ở trên và tôi thực sự thích nó hơn vì nó linh hoạt hơn với những gì chúng tôi đang muốn tạo

Lập trình tạo danh mục trong WordPress

Nói một cách đơn giản, phân loại WordPress là cách phân loại dữ liệu. Điều này có nghĩa là Danh mục và Thẻ đều là các nguyên tắc phân loại mặc dù chúng được sử dụng theo những cách khác nhau

Ngoài ra, nếu bạn kết thúc việc tạo các loại bài đăng tùy chỉnh của riêng mình và muốn tạo một tập hợp các nguyên tắc phân loại tùy chỉnh dành riêng cho loại bài đăng đã nói, thì API WordPress cho phép bạn làm điều này

Cuối cùng, việc sử dụ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' );
2 cho phép chúng tôi tạo danh mục, thẻ và các nguyên tắc phân loại tùy chỉnh khác theo chương trình

Vì lý do này, tôi thích sử dụng chức năng cụ thể này để tạo danh mục hơn bất kỳ thứ gì khác mà WordPress cung cấp

Để tạo danh mục dựa trên mã được xác định trước đó trong bài đăng này, chúng tôi sẽ thực hiện lệnh gọi API sau

function example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );

Làm mới trang sau đó sẽ mang lại kết quả sau

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình

À, chúng ta đi nào

Có một số sắc thái khi sử dụng chức năng này. Cụ thể

Xử lý lỗi được gán cho sự không tồn tại của tham số $taxonomy và $term trước khi chèn. Nếu cả thuật ngữ id và phân loại tồn tại trước đó, thì một mảng sẽ được trả về có chứa thuật ngữ id và nội dung của nội dung được trả về. Các khóa của mảng là ‘term_id’ và ‘term_taxonomy_id’ chứa các giá trị số

Giả định rằng thuật ngữ này chưa tồn tại hoặc điều trên sẽ được áp dụng

Phần còn lại có thể xem lại trong bài viết Codex

Dù sao đi nữa, tôi nhận thấy rằng việc sử dụng wp_insert_term sẽ linh hoạt và rõ ràng hơn nhiều khi tạo danh mục theo chương trình (và rõ ràng là các nguyên tắc phân loại khác) trong nhiều dự án của tôi

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.

  • WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
    SmashingConf Freiburg 2023

  • WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
    Giao diện người dùng SmashingConf 2023

  • WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
    Bắt đầu miễn phí

  • WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
    Các mẫu thiết kế giao diện thông minh, khóa học 9h-video

  • WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
    Hệ thống thiết kế thành công

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

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình

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ỡ “TypeScript trong 50 bài học”, hướng dẫn mới tuyệt vời của chúng tôi về TypeScript. Với các hướng dẫn chi tiết về mã, các ví dụ thực hành và các vấn đề phổ biến — tất cả được chia thành các bài học ngắn, dễ quản lý. Đối với những nhà phát triển biết đủ JavaScript để trở nên nguy hiểm

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

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình

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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    10 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 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 example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );
11 trong chủ đề của mình

function example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );
3

Ở 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 example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );
12 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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    13 Tùy chọn
    function example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    13 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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    15 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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    16 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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    17 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 example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    18 Tùy chọn này thiết lập các điều khiển mặc định của WordPress 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 trong Codex
  • function example_insert_category() {
    	wp_insert_term(
    		'Example Category',
    		'category',
    		array(
    		  'description'	=> 'This is an example category created with wp_insert_term.',
    		  'slug' 		=> 'example-category'
    		)
    	);
    }
    add_action( 'after_setup_theme', 'example_insert_category' );
    
    19 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' );
    30 (theo mặc định)
WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
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' );
31

function example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );
1
WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
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' );
32 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' );
3

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' );
33, 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' );
35 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' );
31 — để tạo hướng dẫn hữu ích và 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' );
2

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' );
3

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' );
37

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
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' );
5

Đ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' );
6

Đâ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' );
7

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' );
38

WordPress thêm danh mục vào loại bài đăng tùy chỉnh theo chương trình
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 example_insert_category() {
	wp_insert_term(
		'Example Category',
		'category',
		array(
		  'description'	=> 'This is an example category created with wp_insert_term.',
		  'slug' 		=> 'example-category'
		)
	);
}
add_action( 'after_setup_theme', 'example_insert_category' );
19 đượ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' );
20 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' );
21 để hiển thị nếu nó tồn tại (trong trường hợp của chúng tôi là ____322). 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' );
23 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' );
24

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' );
25. Để 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' );
6

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' );
26. 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' );
27. 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 phụ 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