brightnucleus / custom-content
由配置驱动的 WordPress 自定义内容定义(自定义文章类型、自定义分类法)。
v0.1.9
2022-02-24 16:23 UTC
Requires
- brightnucleus/config: >=0.4.3
- brightnucleus/contracts: >=0.1.1
- brightnucleus/exceptions: >=0.2.5
- brightnucleus/localization: >=0.1.0
- doctrine/collections: >=1.3
Requires (Dev)
- brain/monkey: ^1
- phpunit/phpunit: ~5
README
由配置驱动的 WordPress 自定义内容定义(自定义文章类型、自定义分类法)。
目录
安装
使用此包的最佳方式是通过 Composer
composer require brightnucleus/custom-content
基本用法
注册新的自定义文章类型
要注册新的自定义文章类型,您需要在配置文件中定义它。默认值可以在 config/defaults.php
配置中找到。然后通过将其配置注入到构造函数中实例化 CustomPostType
类,并调用其 register()
方法。
示例
<?php namespace CPT\Example; use BrightNucleus\Config\ConfigFactory; use BrightNucleus\CustomContent\CustomPostType; // You can of course load your Config from a file. We create one directly here // to make the example clearer. $config = ConfigFactory::create( [ // This configuration key represents the slug of the CPT. 'example' => [ // For most localization needs, it should be sufficient to only provide // these four name variants. The Custom Content component will figure // out the rest. Argument::NAMES => [ Name::SINGULAR_NAME_UC => _x('Example', 'post type uc singular name', 'cpt-example'), Name::SINGULAR_NAME_LC => _x('example', 'post type lc singular name', 'cpt-example'), Name::PLURAL_NAME_UC => _x('Examples', 'post type uc plural name', 'cpt-example'), Name::PLURAL_NAME_LC => _x('examples', 'post type lc plural name', 'cpt-example'), ], // Here, we register the taxonomy we'll later create with our new custom // post type. Argument::TAXONOMIES => [ 'taxexample' ], // We also add some supported features to the custom post type. Argument::SUPPORTS => [ Feature::TITLE, Feature::AUTHOR, Feature::REVISIONS, Feature::COMMENTS, Feature::THUMBNAIL, ], ], ] ); // Create a new `CustomPostType` instance configured by our new Config file. $example_cpt = new CustomPostType( $config ); // Register this new custom post type with WordPress. // Note that CPTs should always be registered within the `init` hook. add_action( 'init', [ $example_cpt, 'register' ] );
注册新的自定义分类法
要注册新的自定义分类法,您需要在配置文件中定义它。默认值可以在 config/defaults.php
配置中找到。然后通过将其配置注入到构造函数中实例化 CustomTaxonomy
类,并调用其 register()
方法。
示例
<?php namespace Tax\Example; use BrightNucleus\Config\ConfigFactory; use BrightNucleus\CustomContent\CustomTaxonomy; // You can of course load your Config from a file. We create one directly here // to make the example clearer. $config = ConfigFactory::create( [ // This configuration key represents the slug of the CPT. 'taxexample' => [ // For most localization needs, it should be sufficient to only provide // these four name variants. The Custom Content component will figure // out the rest. Argument::NAMES => [ Name::SINGULAR_NAME_UC => _x('TaxExample', 'taxonomy uc singular name', 'tax-example'), Name::SINGULAR_NAME_LC => _x('taxexample', 'taxonomy lc singular name', 'tax-example'), Name::PLURAL_NAME_UC => _x('TaxExamples', 'taxonomy uc plural name', 'tax-example'), Name::PLURAL_NAME_LC => _x('taxexamples', 'taxonomy lc plural name', 'tax-example'), ], // Here, we register the taxonomy with our previously created custom // post type. Argument::POST_TYPES => [ 'example' ], ], ] ); // Create a new `CustomTaxonomy` instance configured by our new Config file. $example_tax = new CustomTaxonomy( $config ); // Register this new custom taxonomy with WordPress. // Note that Taxonomies should always be registered within the `init` hook. add_action( 'init', [ $example_tax, 'register' ] );
一次性注册多个自定义内容元素
要一次性注册多个自定义内容元素,您可以实例化一个 CustomContent
对象,并向其传递包含所有自定义文章类型和分类法的配置。
配置的格式与上面的单例配置相似,区别在于它以每个自定义内容类型的键开始,位于前缀之后。不同的别名是相应内容类型的子项。
已知的内容类型包括
CustomPostType
CustomTaxonomy
示例
<?php namespace CustomContent\Example; use BrightNucleus\Config\ConfigFactory; use BrightNucleus\CustomContent\CustomContent; use BrightNucleus\CustomContent\CustomPostType\Argument as CPTArgument; use BrightNucleus\CustomContent\CustomTaxonomy\Argument as TaxArgument; // You can of course load your Config from a file. We create one directly here // to make the example clearer. $config = ConfigFactory::create( [ // In this example, we want to register two custom post types (`book` & // `magazine`) as well as two custom taxonomies related to these // (`publisher`, `shelf`). 'CustomPostType' => [ 'book' => [ // Arguments to define a book. // [...] CPTArgument::TAXONOMIES => [ 'publisher', 'shelf' ], ], 'magazine' => [ // Arguments to define a magazine. // [...] CPTArgument::TAXONOMIES => [ 'publisher', 'shelf' ], ], ], 'CustomTaxonomy' => [ 'publisher' => [ // Arguments to define a publisher. // [...] TaxArgument::POST_TYPES => [ 'book', 'magazine' ], ], 'shelf' => [ // Arguments to define a publisher. // [...] TaxArgument::POST_TYPES => [ 'book', 'magazine' ], ], ], ] ); // Create a new `CustomContent` instance configured by our new Config file. $custom_content = new CustomContent( $config ); // Register this new custom content with WordPress. add_action( 'init', [ $custom_content, 'register' ] );
关于重写规则
如果您的自定义内容包含漂亮的永久链接,您将需要刷新重写规则。
注意:您需要确保只刷新重写规则一次,而不是在每个页面请求时都刷新。
实现此目的的最佳方法是挂钩到插件激活,注册您的自定义内容,然后在该挂钩中调用 flush_rewrite_rules()
。
示例
<?php namespace CustomContent\Example; use BrightNucleus\Config\ConfigFactory; use BrightNucleus\CustomContent\CustomContent; function register_custom_content() { static $custom_content; if ( null === $custom_content ) { $config = ConfigFactory::create( __DIR__ . '/config/custom_content.php' ); $custom_content = new CustomContent( $config ); } $custom_content->register(); } add_action( 'init', __NAMESPACE__ . '\\register_custom_content' ); function flush_rewrite_rules() { cc_example_register_custom_content(); flush_rewrite_rules(); } register_activation_hook( __FILE__, __NAMESPACE__ . '\\flush_rewrite_rules' );
贡献
欢迎所有反馈/错误报告/拉取请求。
许可证
版权(c)2016 Alain Schlesser,Bright Nucleus
此代码根据 MIT 许可证 许可。