wpfulcrum/taxonomy

Fulcrum 分类模块 - 轻松创建自定义分类。

3.0.1 2017-12-12 03:52 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:01:38 UTC


README

Build Status Latest Stable Version License

Fulcrum 自定义分类模块可以让您轻松地将自定义分类添加到项目中。只需传入配置,它就会为您处理剩余的工作。

功能

  • 注册由系统处理。
  • 标签生成 - 当您不需要国际化时很有用。
  • 存储在 Fulcrum 容器中 - 添加后,将自动存储在容器中以供全局使用。

安装

使用此组件的最佳方式是通过 Composer

composer require wpfulcrum/taxonomy

依赖关系

此模块需要

  • 至少 PHP 5.6
  • WordPress 4.8+

配置自定义分类

此模块,如同所有 Fulcrum 模块一样,是作为 ModularConfig 设计模式的一部分进行配置的。在您的主题/插件配置文件夹中,您需要创建一个配置文件。以下是该文件的基本结构

<?php

$config = [
    'autoload'     => true,
    'taxonomyName' => 'genre',
    'config'       => [
        'taxonomyConfig' => [],
        'labelsConfig'   => [],
    ],
];

$config['config']['taxonomyConfig'] = [
    'objectType' => ['book'],

    /**
     * Arguments Configuration Parameters
     *
     * @see https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments for more details.
     *
     * Don't configure the label or labels here.  Those are handled separately below.
     */
    'args'       => [
        'description'       => 'Book Genres',
//        'label'        => '', <- This isn't needed.
//        'labels'       => [], <- don't configure here as they are configured below.
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
    ],
];

/**
 * Labels Builder - Configuration Parameters
 */
$config['config']['labelsConfig'] = [

    /***************************************************************************************************
     * When Your Plugin Doesn't Need Internationalization:
     *
     * By default, the label builder automatically builds the labels for you using the plural and singular
     * names you configure below.
     **************************************************************************************************/
    'useBuilder'   => true, // set to false when you need internationalization.
    'pluralName'   => 'Genres',
    'singularName' => 'Genre',

    /***************************************************************************************************
     * Specify the labels you want here.
     *
     * When not using the automatic builder (i.e. when 'useBuilder' is set to `false`), then you specify
     * all the custom labels here.
     *
     * If you are using the builder, any labels you specify here will overwrite what the builder generates.
     *
     * @see https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments for more details.
     **************************************************************************************************/
    'labels'       => [
        'name'              => _x('Genres', 'taxonomy general name', 'textdomain'),
        'singular_name'     => _x('Genre', 'taxonomy singular name', 'textdomain'),
        'search_items'      => __('Search Genres', 'textdomain'),
        'all_items'         => __('All Genres', 'textdomain'),
        'parent_item'       => __('Parent Genre', 'textdomain'),
        'parent_item_colon' => __('Parent Genre:', 'textdomain'),
        'edit_item'         => __('Edit Genre', 'textdomain'),
        'update_item'       => __('Update Genre', 'textdomain'),
        'add_new_item'      => __('Add New Genre', 'textdomain'),
        'new_item_name'     => __('New Genre Name', 'textdomain'),
        'menu_name'         => __('Genre', 'textdomain'),
    ],
];

return $config;

使其工作

使用此模块有两种方式

  1. 使用完整的 Fulcrum 插件
  2. 或在不使用 Fulcrum 的情况下独立使用。

使用 Fulcrum

Fulcrum 中,您的插件是一个附加组件。在您的插件配置文件中,您将有一个 serviceProviders 参数,其中您将列出您想要使用的每个服务提供商。在这种情况下,您将使用 provider.taxonomy

例如,使用我们上面的图书类型配置,这将是这样配置的

	'serviceProviders' => [

		/****************************
		 * Custom Post Types
		 ****************************/
		'genre.taxonomy' => array(
			'provider' => 'provider.taxonomy', // this is the service provider to be used.
			'config'   => BOOK_PLUGIN_DIR . 'config/taxonomy/genre.php', // path to the genre's taxonomy configuration file.
		),
	],

Fulcrum 的附加组件模块 处理插件激活和停用时刷新重写。这为您节省了时间。

不使用 Fulcrum

在不使用 Fulcrum 的情况下,您需要实例化每个依赖项和 Taxonomy。例如,您会这样做

$config = require_once BOOK_PLUGIN_DIR . 'config/taxonomy/genre.php';  // path to the genre's taxonomy configuration file.

$taxonomy = new PostType(
    $config['taxonomyName'],
    ConfigFactory::create($config['config']['taxonomyConfig']),
    new LabelsBuilder(ConfigFactory::create($config['config']['labelsConfig']))
);
$taxonomy->register();    

您需要处理您的插件激活和停用时的重写刷新。

贡献

欢迎所有反馈、错误报告和拉取请求。