gturpin / post-type-handler
该软件包最新版本(0.2.0)没有提供许可信息。
用于快速管理PostType和Taxonomy声明的辅助类
0.2.0
2023-08-10 16:18 UTC
Requires
- php: >=7.4
README
用于快速管理PostType和Taxonomy声明的辅助类
功能
- 轻松添加新的Post Types或更新现有的Post Types
- 轻松添加新的Taxonomies
- 轻松将Post Types链接到Taxonomies及反之亦然
- 轻松向管理界面添加新列并管理它们(填充、排序、重新排序)
- 轻松向管理界面添加Taxonomy筛选器
安装
使用composer安装
在您的终端中运行以下命令以使用composer安装软件包
composer require gturpin/post-type-handler
该软件包使用自动加载器,因此请务必注册自动加载器。如果您不知道如何操作,请参阅下面的基本示例。
基本用法
以下是一个设置简单PostType的基本示例。
// don't forget to import your autoloader require_once __DIR__ . '/vendor/autoload.php'; use PostTypeHandler\PostType; $post_type_handler = new PostType( 'Book' ); $post_type_handler->register();
您可以这样设置dashicon
$post_type_handler->set_icon( 'dashicons-book' ); // Just the dashicon name also works $post_type_handler->set_icon( 'book' );
您可以向PostType声明中添加自定义$options和$labels
$labels = [ 'add_new' => __( 'my add_new', 'context' ), 'all_items' => __( 'my all_items', 'context' ), ]; $options = [ 'public' => true, 'menu_icon' => 'dashicons-admin-post', 'rewrite' => [ 'slug' => 'my-post-type', ], ]; $post_type_handler = new PostType( 'Book', $options, $labels ); $post_type_handler->register();
如果您已经注册了,您还可以为PostType设置Taxonomies。
$post_type_handler = new PostType( 'Book' ); // add multiple taxonomies $post_type_handler->set_taxonomies( [ 'custom-taxonomy', 'post_tag' ] ); // or add a single taxonomy $post_type_handler->set_taxonomies( 'custom-taxonomy' ); $post_type_handler->register();
否则,您可以注册一个新的Taxonomy,然后将其添加到PostType声明中。
use PostTypeHandler\Taxonomy; // Register the Taxonomy $taxonomy_handler = new Taxonomy( 'custom-taxonomy' ); $taxonomy_handler->register(); // Add it to the PostType in PostType declaration $post_type_handler = new PostType( 'Book' ); $post_type_handler->set_taxonomies( 'custom-taxonomy' ); $post_type_handler->register();
或者,您可以设置Taxonomy到一个已注册的Post Type。
$taxonomy_handler = new Taxonomy( 'custom-taxonomy' ); // This is a post type slug and must be lower case! // Also works with an array and/or variable: [ 'book', $post_type_handler ] $taxonomy_handler->set_post_types( 'book' ); $taxonomy_handler->register();
您可以将Taxonomy对象本身提供给PostType。
$taxonomy_handler = new Taxonomy( 'custom-taxonomy' ); $taxonomy_handler->register(); $post_type_handler = new PostType( 'Book' ); // Also works with an array : [ 'post_tag', $taxonomy_handler ] $post_type_handler->set_taxonomies( $taxonomy_handler ); $post_type_handler->register();
您可以对现有的CPT(例如Post)做同样的事情
$posts = new PostType( 'Post' ); $posts->set_taxonomies( 'custom-taxonomy' ); $posts->register();
您还可以从Post Type中删除Taxonomy。
$posts = new PostType( 'Post' ); $posts->remove_taxonomy( 'post_tag' ); $posts->register();
管理Post Types列
我将解释一些如何为PostType管理列的示例。
- 注册Post Type。
- 操纵列。
- 通过重新注册PostType来保存更改。
添加新列
要向PostType添加新列,您可以这样做
// Call the columns function to get access to the column manager and add a new column $post_type_handler->columns()->add( [ 'custom-slug' => __( 'Custom label', 'context' ), 'year' => __( 'Year', 'context' ), ] ); // You can also pass only one slug and label $post_type_handler->columns()->add( 'custom-slug', 'Custom label' );
隐藏列
要隐藏列,您可以这样做
// Call the columns function to get access to the column manager and hide a built-in column or a custom one $post_type_handler->columns()->hide( [ 'custom-slug', 'date' ] ); // You can also hide only one column $post_type_handler->columns()->hide( 'year' );
设置所有列
您可以通过这种方式一次性设置所有列。这样做时,您必须查看Manage columns钩子,以防止出现不想要的列
// Call the columns function to get access to the column manager and set all columns $post_type_handler->columns()->set( [ 'custom-slug' => __( 'Custom label', 'context' ), 'year' => __( 'Year', 'context' ), ] );
填充列
要填充列,您可以这样做
- 您一次只能填充一个列
- 您必须显示内容而不是返回它
- 您不能使用此功能来填充内置列
// Call the columns function to get access to the column manager and populate a column $post_type_handler->columns()->populate( 'custom-slug', function( $column, $post_id ) { echo get_the_title( $post_id ); } ); $post_type_handler->columns()->populate( 'year', function( $column, $post_id ) { echo get_the_date( 'Y', $post_id ); } );
您也可以向现有和内置的Post Types添加新列
$posts = new PostType( 'Post' ); $posts->columns()->add( 'id' ); $posts->columns()->populate( 'id', function( $column, $post_id ) { echo $post_id; } ); $posts->register();
使列可排序
要使列可排序,您可以这样做
- 您必须在数组中将列slug作为键和值
- 值必须是用于排序的元数据键
- 在使列可排序之前,请务必填充列!
// Call the columns function to get access to the column manager and make a column sortable $post_type_handler->columns()->sortable( [ 'rating' => 'rating', // You can add true to make the sort by numeric order // or false to make it by alphabetical order which is the default 'year' => [ 'year', true ], ] );
排序列
您可能希望对列进行排序,即使是原生列,可以这样做
- 设置最终位置,从0开始
- 避免在您的数组中重复和负位置!
$post_type_handler->columns()->order( [ // Reorder the native columns 'title' => 5, // Use large numbers to be sure that the column will be at the end 'cb' => 15, 'custom-slug' => 1, 'rating' => 3, 'author' => 8, ] );
向编辑屏幕添加分类筛选器
要向编辑屏幕添加分类筛选器,您可以这样做
- 添加分类slug列表
- 顺序很重要,因为筛选器将按此顺序显示!
$post_type_handler->set_taxonomy_filters( [ 'custom-taxonomy', ] );
钩子
待办事项
也可以通过发送对象本身(通过对象本身,可能带有__tostring方法)来添加分类添加管理列的方式隐藏列并为每个Post Type设置默认值向管理屏幕添加新列设置列顺序设置整个列数组使用自定义函数填充任何列可以对每个列按其值进行排序(数字/字母)
添加一个函数,方便添加图标,无需使用 $options 数组添加管理屏幕上的筛选器的方法设置一个数组来排序并保持顺序
添加一个类来管理分类添加新的分类- 可以操作现有的分类(文章标签 & 分类)
可以直接在文章类型上注册(通过 slug 或对象本身,可能需要使用 __tostring 方法)
可以操作现有的文章类型(更新选项和标签)将 @link/author/license 添加到主类中- 相同的列,但用于分类
- 可以删除管理屏幕('post_row_actions')中的行操作(编辑、查看、回收站、删除)
- 检查我们是否可以为添加项做同样的事情
- 检查添加/删除批量编辑操作
- 检查我们是否可以添加/更新/删除批量操作上方的列表(所有/已发布等 ...)