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.
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ìnhfunction 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' ];
3 Tùy chọnfunction 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ơnfunction 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 saufunction 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 Codexfunction 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ạifunction 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' ];
10 [theo mặc đị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' ];
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ìnhTrang 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' ];
04Mộ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àyfunction 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' ];
4Hiể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ã saufunction 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' ];
0Từ 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