WordPress tạo bài đăng tùy chỉnh theo chương trình

WordPress đi kèm với năm loại bài đăng mặc định.

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
1,
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
2,
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
3,
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
4 và
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
5

Trong khi phát triển plugin của mình, bạn có thể cần tạo loại nội dung cụ thể của riêng mình. ví dụ: sản phẩm cho trang web thương mại điện tử, bài tập cho trang web học tập điện tử hoặc phim cho trang web đánh giá

Sử dụng Loại bài đăng tùy chỉnh, bạn có thể đăng ký loại bài đăng của riêng mình. Khi một loại bài đăng tùy chỉnh được đăng ký, nó sẽ có một màn hình quản trị cấp cao nhất mới có thể được sử dụng để quản lý và tạo các bài đăng thuộc loại đó

Để đăng ký loại bài đăng mới, bạn sử dụng hàm register_post_type[]

Cảnh báo.
Chúng tôi khuyên bạn nên đặt các loại bài đăng tùy chỉnh trong plugin thay vì chủ đề. Điều này đảm bảo rằng nội dung người dùng vẫn di động ngay cả khi chủ đề bị thay đổi.

Ví dụ tối thiểu sau đăng ký một loại bài đăng mới, Sản phẩm, được xác định trong cơ sở dữ liệu là

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
6

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __['Products', 'textdomain'],
				'singular_name' => __['Product', 'textdomain'],
			],
				'public'      => true,
				'has_archive' => true,
		]
	];
}
add_action['init', 'wporg_custom_post_type'];

Vui lòng truy cập trang tham khảo cho register_post_type[] để biết mô tả về các đối số

Điều quan trọng là bạn phải đặt trước các chức năng và số nhận dạng loại bài đăng của mình bằng một tiền tố ngắn tương ứng với plugin, chủ đề hoặc trang web của bạn

Cảnh báo.
Đảm bảo số nhận dạng loại bài đăng tùy chỉnh của bạn không vượt quá 20 ký tự vì cột

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
7 trong cơ sở dữ liệu hiện là trường VARCHAR có độ dài đó.

Cảnh báo.
Để đảm bảo khả năng tương thích về phía trước, không sử dụng wp_ làm số nhận dạng của bạn — nó đang được lõi WordPress sử dụng.

Cảnh báo.
Nếu số nhận dạng của bạn quá chung chung [ví dụ:. “

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
8“], nó có thể xung đột với các plugin hoặc chủ đề khác đã chọn sử dụng cùng mã định danh đó.

Ghi chú.
Không thể giải quyết các định danh loại bài đăng trùng lặp mà không vô hiệu hóa một trong các loại bài đăng xung đột.

Loại bài đăng tùy chỉnh có sên riêng trong cấu trúc URL của trang web

Theo mặc định, bài đăng loại

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
6 sẽ sử dụng cấu trúc URL sau.
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
1

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
6 là sên của loại bài đăng tùy chỉnh của bạn và
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
3 là sên của sản phẩm cụ thể của bạn

Permalink cuối cùng sẽ là.

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
4

Bạn có thể thấy liên kết cố định trên màn hình chỉnh sửa cho loại bài đăng tùy chỉnh của mình, giống như với các loại bài đăng mặc định

Để đặt sên tùy chỉnh cho sên của loại bài đăng tùy chỉnh của bạn, tất cả những gì bạn cần làm là thêm cặp khóa => giá trị vào khóa

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
5 trong mảng đối số
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
6

Ví dụ

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];

Ở trên sẽ dẫn đến cấu trúc URL sau.

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
7

Cảnh báo.
Việc sử dụng sên chung chung như

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
8 có khả năng xung đột với các plugin hoặc chủ đề khác, vì vậy hãy thử sử dụng plugin hoặc chủ đề cụ thể hơn cho nội dung của bạn.

Ghi chú.
Không giống như số nhận dạng loại bài đăng tùy chỉnh, vấn đề sên trùng lặp có thể được giải quyết dễ dàng bằng cách thay đổi sên cho một trong các loại bài đăng xung đột.

Nếu tác giả plugin bao gồm lệnh gọi

function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
9 trên các đối số, điều này có thể được thực hiện theo chương trình bằng cách ghi đè các đối số được gửi qua hàm
function wporg_custom_post_type[] {
	register_post_type['wporg_product',
		array[
			'labels'      => array[
				'name'          => __[ 'Products', 'textdomain' ],
				'singular_name' => __[ 'Product', 'textdomain' ],
			],
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array[ 'slug' => 'products' ], // my custom slug
		]
	];
}
add_action['init', 'wporg_custom_post_type'];
6

Chủ Đề