gin0115/cpt

一个用于在WordPress中注册自定义文章类型及其分类的小助手类

维护者

详细信息

github.com/gin0115/cpt

源代码

1.1.1 2022-01-25 13:26 UTC

This package is auto-updated.

Last update: 2024-09-25 20:00:28 UTC


README

通过将更改应用于项目的 vendor 目录来创建分支,以利用这些更改。

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 转换为 resourcescountry 转换为 countriespotato 转换为 potatoesgoose 转换为 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或更高版本的许可证下发布。