aurooba/cpt

WordPress中注册自定义文章类型及其分类的小助手类

v1.1.0 2021-03-04 18:00 UTC

This package is auto-updated.

Last update: 2024-09-16 09:00:43 UTC


README

Packagist Downloads

Packagist PHP Version Support Packagist License

自定义文章类型和分类生成器

在WordPress项目中快速注册自定义文章类型及其关联的分类,并带有智能默认设置。

有关自定义文章类型注册的完整文档,请参阅https://developer.wordpress.org/reference/functions/register_post_type/

有关分类注册的完整文档,请参阅https://developer.wordpress.org/reference/functions/register_taxonomy/

最低要求

  • PHP 7.0
    • 推荐使用PHP 7.4+
  • WordPress 5.0
    • 已测试至WP 5.6.2

安装

最简单的方法是通过Composer安装:

composer require aurooba/cpt

确保您已在添加包的地方设置了composer自动加载。

对于主题,在您的functions.php文件中:

require get_template_directory() . '/vendor/autoload.php';

对于插件,在您的主要插件文件中,靠近顶部:

require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';

使用方法

没有看到您常用且希望更易用的选项?请打开一个问题或在讨论区域中提出,让我们一起实现它。 :)

关于您的特定用例有任何问题?在支持讨论中提问。

基本使用

// Yep feel free to use spaces in your name
$custom_post_type = new Aurooba\CPT( 'custom post type' );

默认情况下,该类只接受一个参数:一个单数名称,如resource。它足够智能,可以将resource转换为resources,将country转换为countries,将potato转换为potatoes,将goose转换为geese,并处理一些异常。

目前,它还不能处理诸如riceteaknowledge等不可数名词。这是一个计划中的新增功能,将在以后添加。

定制使用

$custom_post_type = new Aurooba\CPT(
	'singular name', // singular taxonomy name, human readable
	$args, // an array of custom parameters for the custom post type
	$labels // an array of custom labels for the custom post type
);

$custom_post_type->add_taxonomy(
	'Taxonomy', // singular taxonomy name
	// array of custom parameters for the taxonomy
	array(
		'hierarchical' => false,
	),
	$labels, // array of custom labels for the taxonomy
)

图标

默认图标是在类中设置好的screenoptions Dashicon,纯粹因为我喜欢。

您可以将不同的Dashicon传递给类,如这个不同图标示例所示。

您还可以传递自定义SVG图标,如这个自定义svg图标示例所示。

分类

该类还可以将现有分类附加到CPT生成新的分类附加到CPT

示例

注册一个基本的自定义文章类型

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts() {

	$resource = new Aurooba\CPT( 'resource');

}

add_action( 'after_setup_theme', 'initialize_cpts' );

注册并将基本分类与自定义文章类型相关联

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts_and_taxonomies() {

	// initialize cpt
	$resource = new Aurooba\CPT( 'resource' );

	// add Resource Type taxonomy
	$resource->add_taxonomy( 'Resource Type' );

}

add_action( 'after_setup_theme', 'initialize_cpts_and_taxonomies' );

使用不同图标注册自定义文章类型

$resource = new Aurooba\CPT(
		'resource',
		array( 'menu_icon' => 'dashicons-share-alt' ),
	);

使用自定义图标注册自定义文章类型

// Your icon SVG Code. Alternatively, you can pass the path to an SVG file.
$resource_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path fill="#ffffff" d="M608 0H160c-17.67 0-32 13.13-32 29.33V112h48V48h48v64h48V48h224v304h112c17.67 0 32-13.13 32-29.33V29.33C640 13.13 625.67 0 608 0zm-16 304h-48v-56h48zm0-104h-48v-48h48zm0-96h-48V48h48zM128 320a32 32 0 1 0-32-32 32 32 0 0 0 32 32zm288-160H32a32 32 0 0 0-32 32v288a32 32 0 0 0 32 32h384a32 32 0 0 0 32-32V192a32 32 0 0 0-32-32zm-16 240L299.31 299.31a16 16 0 0 0-22.62 0L176 400l-36.69-36.69a16 16 0 0 0-22.62 0L48 432V208h352z"/></svg>';

$resource = new Aurooba\CPT(
		'resource',
		array(
			'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $resource_icon ) ),
	);

创建一个定制的自定义文章类型

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts() {

	// create a block template to include in the custom post type
	$block_template = array(
		array(
			'core/heading',
			array(
				'level'       => 2,
				'placeholder' => __( 'Resource Type', 'textdomain' ),
			),
		),
		array(
			'core/paragraph',
			array(
				'content' => __( 'Places where this resource can be helpful are:>', 'textdomain' ),
			),
		),
	);

	$resource = new Aurooba\CPT(
		'resource',
		array(
			'menu_position' => 26,
			'menu_icon'     => 'dashicons-plus-alt',
			'template'      => $block_template,
			'rewrite'       => true,
			'capability_type' => 'resource',
			'map_meta_cap' => true,
			'supports'      => array(
				'title',
				'editor',
				'thumbnail',
				'author',
				'custom-fields',
			),
		),
	);

}

add_action( 'after_setup_theme', 'initialize_cpts' );

创建一个定制的分类

	$resource->add_taxonomy(
		'Resource Type',
		array(
			'capabilities' => array(
				'manage_terms' => 'manage_resource_type',
				'edit_terms'   => 'edit_resource_type',
				'delete_terms' => 'delete_resource_type',
				'assign_terms' => 'assign_resource_type',
			),
		),
	);

附加现有分类

$resource->add_taxonomy( 'Post Tag' );
$resource->add_taxonomy( 'Category' );

贡献

欢迎贡献者!来帮助扩展、测试和改进这个包,使其成为一个极其有用的库。更多信息请查看CONTRIBUTING.md

许可证

由Aurooba Ahmed创建。根据GPL v2或更高版本许可。