pelmered / post-types-creator
WordPress中轻松创建本地化准备好的自定义文章类型和自定义分类的辅助插件
Requires
This package is auto-updated.
Last update: 2024-01-30 15:16:21 UTC
README
提供简单界面的辅助插件,只需几行代码即可根据最佳实践创建完全翻译的自定义文章类型和分类。
内容
##功能
- 简单界面,只需几行代码即可。
- 灵活。不限制任何内容,您可以通过正常方式指定任何参数传递给
register_post_type()
和register_taxonomy()
,它们将覆盖默认值。 - 本地化准备就绪(WP Admin 中的所有标签都将被翻译,您只需指定文章类型的单数和复数形式)。当前支持以下语言:
- 英语
- 瑞典语
- 挪威语
- 请帮助我添加更多!
- 自动生成翻译的永久链接/别名,但可以覆盖此默认值。
- 仅用几行代码即可在管理员的自定义列中添加
- 轻松添加自定义文章状态
- 拖放排序(在 WP Admin 中的常规列表视图中拖放排序,适用于文章和分类/术语)
- 与高级自定义字段(可选)集成
##计划中的 2.0 版本功能
- 代码结构和质量的全面重写
- 更多单元测试(初始目标是至少 50% 的代码覆盖率)
- WP-CLI 的脚手架命令用于生成插件
- 使用 WordPress 4.4+ 的新术语元功能进行排序
##常见问题及故障排除
如果您了解基本的 PHP,则此插件非常易于使用。您可能遇到的大部分问题都与 WordPress 内置的永久链接缓存或排序混乱或不存在元数据有关(文章不会在 wp-admin 中的文章列表中显示)。要解决这两个问题,只需在任何 wp-admin 中的 URL/查询字符串中添加 &ptc-reinit=1
以运行强制重新初始化,例如:http://example.com/wp-admin/edit.php?post_type=page&ptc-reinit=1
##安装
###Composer 将存储库添加到 composer.json 中的 require 部分,并将 pelmered/post-types-creator
添加到 require 部分。以下是一个典型的完整的 composer.json 文件示例
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/pelmered/post-types-creator"
}
],
"require": {
"pelmered/post-types-creator": "dev-master"
},
"extra": {
"wordpress-install-dir": "public/wp",
"installer-paths": {
"public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"]
}
}
}
注意:编辑安装路径以反映您的安装
###常规手动安装 首先,像常规插件一样安装插件,将插件上传到您的插件文件夹,通常为 wp-content/plugins/
。
###使用示例插件作为自定义文章类型插件的原型 第二次,从 example-plugin/my-custom-post-types/
在此插件中复制示例插件到您的插件文件夹,通常为 wp-content/plugins/
,或从 example-plugin/my-custom-post-types.zip
中的 zip 文件安装示例插件。更改插件名称、描述等,并根据您的需求编辑数据。
##使用方法
###添加自定义文章类型
####最小化:注册一个带有 stores
别名的文章类型,其标签根据 文章类型复数
(复数)和 文章类型单数
(单数)进行翻译。
$options = array( // Use ACF for storing meta data 'use_acf' => true ); $ptc = new \PE\Post_Types_Creator(); $text_domain = 'text-domain'; $ptc->set_post_types(array( 'stores' => array( 'singular_label' => _x('store', 'Post type singular', $text_domain), 'plural_label' => _x('stores', 'Post type plural', $text_domain) ) )); add_action( 'init', array($ptc, 'init'), 0 );
#####示例/典型:与最小化相同,但还添加了描述,使其在管理列表中可拖拽排序,添加自定义管理列,并覆盖一些 register_post_type()
默认值,例如连接到分类法 area
(见下面的示例)。
$ptc = new \PE\Post_Types_Creator(); $text_domain = 'text-domain'; $ptc->set_post_types(array( 'stores' => array( 'singular_label' => _x('store', 'Post type singular', $text_domain), 'plural_label' => _x('stores', 'Post type plural', $text_domain), 'description' => _x('All company stores', 'Post type description', $text_domain), // Make post type drag and drop sortable in admin list view (default: false) 'sortable' => true, 'taxonomy_filters' => true, 'admin_columns' => array( 'image' => array( //Column header/label 'label' => 'Image', //In what position should the column be (optional) 'location' => 2, //callback for column content. Arguments: $post_id 'cb' => 'example_get_featured_image_column' ) ) // Override any defaults from register_post_type() // http://codex.wordpress.org/Function_Reference/register_post_type 'supports' => array( 'title', 'editor', 'thumbnail',), 'taxonomies' => array( 'area' ), ) )); add_action( 'init', array($ptc, 'init'), 0 ); function example_get_featured_image_column( $post_id ) { echo get_the_post_thumbnail( $post_id, 'thumbnail' ); }
####添加分类法:典型的分类法,在正常管理列表视图中可拖拽排序,并且在上面的示例中与 stores
文章类型相关联。
$ptc = new \PE\Post_Types_Creator(); $text_domain = 'text-domain'; $ptc->set_taxonomies(array( 'area' => array( 'singular_label' => _x('area', 'Post type singular', $text_domain), 'plural_label' => _x('areas', 'Post type plural', $text_domain), 'description' => _x('Areas for grouping stores', 'Post type description', $text_domain), 'post_type' => 'stores', // Make post type drag and drop sortable in admin list view (default: false). Affects all get_terms()-queries 'sortable' => true, // Override any defaults from register_taxonomy() // http://codex.wordpress.org/Function_Reference/register_taxonomy ) )); add_action( 'init', array($ptc, 'init'), 0 );
更多示例或帮助开始,请查看 example/example-plugin.php
中的示例插件。将示例插件复制到您的插件目录以快速开始。
##文档
从核心或其他插件添加功能到文章类型
为 WooCommerce 产品类别添加排序功能的示例
$ptc->set_taxonomies(array( 'product_cat' => array( 'register' => false, 'post_type' => array( 'product' ), 'sortable' => true, ), ));
标签 & 描述
您只需要提交文章类型的单数和复数版本标签,所有其他标签都会自动生成。
如果您想覆盖某些内容,可以像这样挂钩到过滤器 pe_ptc_post_type_labels
add_filter( 'pe_ptc_post_type_labels', function( $generated_args, $post_type_slug, $post_type_args ) { // do what you want with $generated_args //And then return it back to the plugin return $generated_args; }, 10, 3)
$post_type['singular_label_ucf'] = ucfirst($post_type['singular_label']); $post_type['plural_label_ucf'] = ucfirst($post_type['plural_label']); $generated_args = array( 'label' => __( $post_slug, $this->text_domain ), 'description' => __( $post_type['plural_label_ucf'], $this->text_domain ), 'labels' => array( 'name' => _x( $post_type['plural_label_ucf'], 'Post Type General Name', $this->text_domain ), 'singular_name' => _x( $post_type['singular_label_ucf'], 'Post Type Singular Name', $this->text_domain ), 'menu_name' => __( $post_type['plural_label_ucf'], $this->text_domain ), 'parent' => sprintf(__( 'Parent %s', $this->text_domain ), $post_type['singular_label']), //'parent_item_colon' => sprintf(__( 'Parent %s:', $this->text_domain ), $post_type['singular_label']), 'all_items' => sprintf(__( 'All %s', $this->text_domain ), $post_type['plural_label']), 'view' => sprintf(__( 'View %s', $this->text_domain ), $post_type['singular_label']), 'view_item' => sprintf(__( 'View %s', $this->text_domain ), $post_type['singular_label']), 'add_new' => sprintf(__( 'Add %s', $this->text_domain ), $post_type['singular_label']), 'add_new_item' => sprintf(__( 'Add new %s', $this->text_domain ), $post_type['singular_label']), 'edit' => __( 'Edit', $this->text_domain ), 'edit_item' => sprintf(__( 'Edit %s', $this->text_domain ), $post_type['singular_label']), 'update_item' => sprintf(__( 'Update %s', $this->text_domain ), $post_type['singular_label']), 'search_items' => sprintf( __('Search %s', $this->text_domain), $post_type['plural_label']), 'not_found' => sprintf(__( 'No %s found', $this->text_domain ), $post_type['plural_label']), 'not_found_in_trash' => sprintf(__( 'No %s found in trash', $this->text_domain ), $post_type['plural_label']), ), );
额外参数
admin_columns
sortable
分类法过滤器
传递 true
以创建文章类型的所有分类法的过滤器
'taxonomy_filters' => false,
覆盖 register_post_status()
默认值
您可以传递到 register_post_status()
的所有默认值和参数