whodunit / post-type-handler
该包最新版本(0.2.1)没有提供许可证信息。
用于快速管理PostType和Taxonomy声明的辅助类
0.2.1
2023-08-11 13:51 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-09-11 16:54:46 UTC
README
用于快速管理PostType和Taxonomy声明的辅助类
功能
- 轻松添加新的PostType或更新现有PostType
- 轻松添加新的Taxonomy
- 轻松将PostType与Taxonomy链接,反之亦然
- 轻松向管理员添加新列并管理它们(填充、排序、重新排序)
- 轻松向管理员添加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设置Taxonomy。
$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到一个已注册的PostType。
$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();
您还可以从PostType中删除Taxonomy。
$posts = new PostType( 'Post' ); $posts->remove_taxonomy( 'post_tag' ); $posts->register();
管理PostType列
我将解释一些如何管理PostType列的示例。
- 注册PostType
- 操作列
- 通过重新注册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' );
设置所有列
您可以通过这样做一次设置所有列。为此,您必须查看管理列钩子,以防止出现不想要的列
// 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 ); } );
您也可以向现有和内置的PostType添加新列
$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, ] );
向编辑屏幕添加Taxonomy过滤器
要向编辑屏幕添加Taxonomy过滤器,您可以这样做
- 添加Taxonomy slugs的列表
- 顺序很重要,因为过滤器将按此顺序显示!
$post_type_handler->set_taxonomy_filters( [ 'custom-taxonomy', ] );
钩子
待办事项
也可以通过发送对象本身(通过对象本身,可能通过__tostring方法)添加Taxonomy添加管理列的方式隐藏列和每个PostType的默认值向管理屏幕添加新列设置列顺序设置整个列数组使用自定义函数填充任意列可以按每个列的值进行排序(数值/字母顺序)
添加一个函数,便于添加图标而不使用$options数组添加一种管理屏幕管理员上的筛选器的方法设置一个数组来排序它们并保持顺序
添加一个类来管理分类法添加新的分类法- 可以处理现有的分类法(post_tag & category)
可以直接在帖子类型上注册(通过slug或对象本身,可能需要使用__tostring方法)
可以处理现有的帖子类型(更新选项和标签)将@link/author/license添加到主类中- 与分类法相同的列
- 可以从管理员屏幕('post_row_actions')中删除行操作(编辑、查看、回收站、删除)
- 检查我们是否可以为添加操作做同样的事情
- 检查是否可以添加/删除批量编辑操作
- 检查我们是否可以在批量操作上方添加/更新/删除列表(全部/已发布等...)