tomekwi / super-cpt
This package is not auto-updated.
Last update: 2024-09-24 05:45:38 UTC
README
注意:此仓库的master分支是夜间构建,请谨慎使用。
WordPress插件,用于构建极其简单且吸引人的自定义文章类型、自定义文章元数据和自定义分类法。您可以从WordPress.org插件仓库安装它。
描述
SuperCPT是自定义文章类型、自定义分类法和自定义文章元数据的一个对象包装器,“由程序员为程序员编写”。简单来说,SuperCPT
- 简化了自定义文章类型和自定义分类法的流程(例如,自动将名称添加到所有标签中),
- 允许您为所有自定义文章类型和分类法设置默认选项,
- 大大简化了创建、保存和显示自定义文章元数据的流程,
- 非常性感!您的自定义字段被设计得非常美观,SuperCPT还包含了来自Font Awesome的361个精美图标(并支持添加您自己的图标)
演示视频
观看一个包含简要演示和说明的屏幕录制。
更多
说明
根据您何时何地声明自定义文章类型和分类法,您有不同的选项来挂钩不同的操作。最安全的选择是使用after_setup_theme,但如果您在其他插件中引用此操作,则plugins_loaded是一个不错的选择。为了避免出现致命错误,在引用之前,您应该检查类Super_Custom_Post_Type是否存在。下面是参考代码。
自定义文章类型
要定义一个新的自定义文章类型,使用字符串为文章类型实例化Super_Custom_Post_Type类。例如,
$movies = new Super_Custom_Post_Type( 'movie' );
它的工作方式非常类似于register_post_type。您通过使用这种方式获得的第一件事是,所有标签都使用“电影”或“电影”进行了设置。如果我们的文章类型是“indie-film”,则标签将是“Indie Film”和“Indie Films”。当然,您也有能力在诸如goose/geese的情况中设置复数词。您还可以通过过滤器定义自己的自定义文章类型默认值。最后,您还可以访问Super_Custom_Post_Type的父类Super_Custom_Post_Meta,用于快速、干净、直观的自定义文章元数据,我们将在下面进行介绍。
最后,如果您构建了大量的自定义文章类型,您可能已经厌倦了那个推针图标。SuperCPT包含了来自Font Awesome的361个精美图标,实现起来非常简单。下面是它的样子
$movies->set_icon( 'film' );
自定义分类法
要定义一个新的自定义分类法,与自定义文章类型类似,您使用字符串为术语名称实例化Super_Custom_Taxonomy。例如
$actors = new Super_Custom_Taxonomy( 'actor' );
再次,我们通过使用“Actor”或“Actors”来获取免费标签,无需分别指定16个标签。
自定义文章元数据
自定义文章元数据是SuperCPT最耀眼的地方,因为这个过程通常是最耗时的。《Super_Custom_Post_Meta》是一个可以添加到任何文章类型(包括内置文章类型和页面)的自立类。这个类有一个add_meta_box方法,负责大部分工作,并且在一定程度上模仿了WordPress功能。以下是一个示例
$movies->add_meta_box( array(
'id' => 'features',
'fields' => array(
'tagline' => array( 'type' => 'text' )
)
) );
add_meta_box方法接收一个参数数组(与核心函数接收正常顺序参数不同)。id是唯一必需的属性,它将成为元框的ID以及标题(这将转换为标题“单词”,例如"movie_details"将转换为“电影详情”)。fields是元框中所有字段的数组。它是一个关联数组,其中数组的键是字段名称,值是字段属性的另一个关联数组。键紧密反映结果字段中的HTML属性,任何插件不认识的键实际上将成为HTML属性(例如,传递'data-src' => 'foo'将变为字段中的HTML属性data-src="foo")。请参阅参考文档,了解add_meta_box参数数组和字段数组的完整选项。
简而言之,使用这个类意味着你不需要做任何额外的工作来存储数据、检索数据、设置框样式等。
辅助函数
SuperCPT提供了一些辅助函数来显示和处理你的文章元数据。
示例代码
以下是完整的示例代码
function scpt_demo() {
if ( ! class_exists( 'Super_Custom_Post_Type' ) )
return;
$demo_posts = new Super_Custom_Post_Type( 'demo-post' );
# Test Icon. Should be a square grid.
$demo_posts->set_icon( 'th-large' );
# Taxonomy test, should be like tags
$tax_tags = new Super_Custom_Taxonomy( 'tax-tag' );
# Taxonomy test, should be like categories
$tax_cats = new Super_Custom_Taxonomy( 'tax-cat', 'Tax Cat', 'Tax Cats', 'category' );
# Connect both of the above taxonomies with the post type
connect_types_and_taxes( $demo_posts, array( $tax_tags, $tax_cats ) );
# Add a meta box with every field type
$demo_posts->add_meta_box( array(
'id' => 'demo-fields',
'context' => 'normal',
'fields' => array(
'textbox-demo' => array(),
'textarea-demo' => array( 'type' => 'textarea' ),
'wysiwyg-demo' => array( 'type' => 'wysiwyg' ),
'boolean-demo' => array( 'type' => 'boolean' ),
'checkboxes-demo' => array( 'type' => 'checkbox', 'options' => array( 'one', 'two', 'three' ) ),
'radio-buttons-demo' => array( 'type' => 'radio', 'options' => array( 'one', 'two', 'three' ) ),
'select-demo' => array( 'type' => 'select', 'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ) ),
'multi-select-demo' => array( 'type' => 'select', 'options' => array( 'one', 'two', 'three' ), 'multiple' => 'multiple' ),
'date-demo' => array( 'type' => 'date' ),
'label-override-demo' => array( 'label' => 'Label Demo' )
)
) );
# Add another CPT to test one-to-one (it could just as easily be one-to-many or many-to-many) relationships
$linked_posts = new Super_Custom_Post_Type( 'linked-post', 'Other Post', 'Other Posts' );
$linked_posts->add_meta_box( array(
'id' => 'one-to-one',
'title' => 'Testing One-to-One relationship',
'context' => 'side',
'fields' => array(
'demo-posts' => array( 'type' => 'select', 'data' => 'demo-post' ),
'side-wysiwyg' => array( 'type' => 'wysiwyg' )
)
) );
$linked_posts->set_icon( 'cogs' );
}
add_action( 'after_setup_theme', 'scpt_demo' );
作者
Matthew Boynes
版权和许可
版权 2012 Matthew Boynes
根据Apache许可证2.0版本(“许可证”);除非您遵守许可证,否则不得使用此作品。您可以在LICENSE文件中或以下地址获得许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则根据许可证分发的软件按“现状”基础分发,不提供任何明示或暗示的保证。有关许可证的具体语言、权限和限制,请参阅许可证。