concept-image/wp-custom-post-types
最新稳定版本:3.1.5
Composer 安装命令:
composer require concept-image/wp-custom-post-types
包简介
Register custom post types and taxonomies in WordPress.
README 文档
README
Requirements
- PHP >= 8.0
- WordPress >= 6.7
- Roots Bedrock >= 1.26
- Roots Acorn >= 4.3
Installation
You can install this package with Composer:
composer require concept-image/wp-custom-post-types
Usage
This package provides a convenient way to create and manage custom post types and taxonomies in WordPress using a structured approach. It leverages the power of Roots Bedrock and Acorn to integrate seamlessly into your WordPress project.
Custom Post Types
To create a custom post type, you need to define a class in the app/CustomPostTypes directory. The class should extend PostType and have a protected static $name property and a getArgs method that returns the arguments for the custom post type registration.
Example:
namespace App\CustomPostTypes;
use ConceptImage\WpCustomPostTypes\PostType;
class Book extends PostType
{
protected static string $name = 'book';
public function getArgs(): array
{
return [
'label' => 'Books',
'public' => true,
'supports' => ['title', 'editor', 'thumbnail'],
'taxonomies' => ['genre'],
];
}
}
Custom Taxonomies
To create a custom taxonomy, you need to define a class in the app/Taxonomies directory. The class should extend Taxonomy and have a protected static $name property, an $object_type property, and a getArgs method.
Example:
namespace App\Taxonomies;
use ConceptImage\WpCustomPostTypes\Taxonomy;
class Genre extends Taxonomy
{
protected static string $name = 'genre';
public array $object_type = ['book'];
public function getArgs(): array
{
return [
'label' => 'Genres',
'public' => true,
'hierarchical' => true,
];
}
}
Removing Base Slug from URLs
If you want to remove the base slug from your custom post type URLs (e.g., /book/my-book becomes /my-book), you can use the RemoveBaseSlug attribute on your custom post type class:
use ConceptImage\WpCustomPostTypes\Attributes\RemoveBaseSlug;
#[RemoveBaseSlug]
class Book
{
...
}
Taxonomies
To create a custom taxonomy, you need to define a class in the app/Taxonomies directory. The class should have a name property, an object_type property, and a getArgs method that returns the arguments for the taxonomy registration.
Example:
namespace App\Taxonomies;
class Genre
{
public $name = 'genre';
public $object_type = ['book'];
public function getArgs(): array
{
return [
'label' => 'Genres',
'public' => true,
'hierarchical' => true,
];
}
}
Registering Custom Post Types and Taxonomies
The package automatically registers custom post types and taxonomies defined in the app/CustomPostTypes and app/Taxonomies directories, respectively. You don't need to manually register them in your theme or plugin.
Migrate from V2 to V3
Run following command :
wp acorn custom-post-types:migration
Changelog
Please refer to CHANGELOG for more information.
Contributing
Please refer to CONTRIBUTING for more information.
License
Please refer to LICENSE for more information.
统计信息
- 总下载量: 1.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-19