93devs / custom-post-type
93digital 提供的自定义帖子类型实用函数
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2021-07-29 14:22:43 UTC
README
一个用于轻松创建 WordPress 自定义帖子类型的 PHP 类
安装
使用 composer 安装
在您的终端中运行以下命令以使用 Composer 安装 PostTypes。
$ composer require "93digital/custom-post-type @dev"
以下是一个使用该类的入门基本示例,但您的设置可能因您如何使用 composer 而有所不同。
<?php
require __DIR__ . '/vendor/autoload.php';
use Nine3\PostType;
$books = new PostType( 'book' );
手动安装
下载项目并将主类文件加载到您的主题 functions.php 中,如下所示
require_once 'custom-post-type/class-nine3-custom-post-type.php';
帖子类型
function __construct( $singular, $plural = null, $icon = 'dashicons-format-aside', $supports = array(), $options = array() )
参数
- $singular (字符串|必需) 单数名称。
- $plural (字符串) 复数名称,默认情况下将在单数名称后附加 's'。
- $icon (字符串) dashicon 名称或自定义 URL。
- $supports (数组) "supports" 参数。默认:标题、编辑器和缩略图。
- $options (数组) 传递给 register_post_type 的参数数组(有关所有可用选项,请参阅 WordPress 编码手册)
创建新的帖子类型
通过简单地将帖子类型名称传递给类构造函数,可以创建新的帖子类型。
$books = new PostType( 'Book' );
定义复数和图标
$books = new PostType(
'Book',
'Books',
'dashicons-admin-page'
);
设置 'supports' 参数
默认情况下,新的自定义帖子类型设置以下参数
- 标题
- 编辑器
- 缩略图
要覆盖/更改,请传递一个数组作为第 4 个参数,如下所示
$books = new PostType(
'Book',
'Books',
'dashicons-admin-page',
array(
'title',
)
);
添加选项
您可以传递 register post type 函数支持的所有额外参数
$books = new PostType(
'Book',
'Books',
'dashicons-admin-page',
array(
'title',
),
array(
'public' => false,
)
);
所有可用选项均列在 WordPress Codex 上
添加分类法
通过使用 taxonomy() 方法,可以轻松地将分类法添加到帖子类型。
创建新的分类法
要创建新的分类法,只需将分类法名称传递给 taxonomy() 方法。标签和分类法短横线由分类法名称生成。
function $books->taxonomy( string $singular_name, string $plural, array args );
参数
- $singular (字符串|必需) 单数名称。
- $plural (字符串) 复数名称,默认情况下将在单数名称后附加 's'。
- args(数组)参数数组。参数。
示例
$books->taxonomy( 'Genre' );
定义名称
您可以通过传递它作为第二个参数来定义复数名称。
- 单数是文章类型的单数标签
- 复数是文章类型的复数标签
- slug是在永久链接中使用的文章类型slug
$books->taxonomy( 'Genre', 'Genres' );
添加选项
您可以通过将选项数组作为方法的第三个参数传递来进一步自定义分类。
$options = array(
'heirarchical' => false
);
$books->taxonomy( 'Genre', 'Genres', $options );
所有可用选项都在WordPress Codex
设置页面
当将'has_archive'参数设置为true时,类将为新的CPT添加一个子页面,称为
[CPT复数] 设置
此页面可以用于使用ACF添加字段,并将显示在选项页面选项中。
要检索存储在自定义页面中的信息,请使用
get_field( '[THE OPTION SLUG]', 'cpt-[SINGULAR NAME]' );
the_field( '[THE OPTION SLUG]', 'cpt-[SINGULAR NAME]' );
第二个属性与设置页面URL的"page"相同
示例
get_field( 'year', 'cpt-book' );
the_field( 'year', 'cpt-book' );
默认情况下,'has_archive'参数设置为true。
循环项目
该类具有内置方法,可以轻松循环自定义文章,而无需手动调用WP_Query类。
注意:出于性能考虑,"no_found_rows"设置为true。如果需要分页,则需要将其设置为true。
简单循环
要使用默认的WP_Query参数简单循环项目,只需使用之前注册的变量即可
<h1>This is a simple loop</h1>
<?php while ( $books->have_posts() ) : $books->the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
在此示例中,$books是用于在WP中注册自定义文章类型的变量名称。
获取文章
以下函数允许自定义用于简单循环的WP_Query参数。
使用自定义参数获取CPT文章。
function get_posts( $posts_per_page = 0, $args );
- $posts_per_page(整数)每页要显示的文章数。默认值 = 0(不设置WP_Query参数)
- $args(数组)传递给WP_Query的参数数组
示例
// Get the first 10 books.
$books->get_posts( 10 );
// Passing offset attribute.
$books->get_posts( 10, array( 'offset' => 11 ) );
// If don't want to specify the post limit, just pass 0 as first parameter.
$books->get_posts( 0, array( 'offset' => 11 ) );
与简单循环一起使用
$books->get_posts( 0, array( 'offset' => 11 ) );
while ( $books->have_posts() ) : $books->the_post();
...
endwhile;
如果需要分页
$books->get_posts( 0, array( 'offset' => 11, 'no_found_rows' => true ) );
while ( $books->have_posts() ) : $books->the_post();
...
endwhile;
通过元值获取文章
$books->get_posts_by_meta( string $meta_key, mixed $meta_value, string $meta_compare, int $post_limit = 0 );
通过分类获取文章
对于使用内置的创建新分类方法注册的所有分类,都提供了以下虚拟方法
$books->get_posts_by_[taxonomy-slug]( string|array values );
其中
- $books是之前用于注册自定义文章类型的变量名称。
- taxonomy_slug 是我们想要过滤的类别的 slug。
- $values 是要过滤的 slug 列表。
示例
假设我们为 cpt 注册了 genre 类别
$books = new PostType( 'Book' );
$books->taxonomy( 'Genre' );
现在我们可以轻松过滤我们的文章
$book_cpt->get_posts_by_genre( 'horror' );
如果需要,我们也可以传递一个包含多个值的 slug 数组进行过滤
单个类别
通过单个类别进行过滤的另一种方法是
$books->get_posts_by_term( string $taxonomy, string|int|array $slugs, string $field = 'slug', array $args = array() );
参数
- $taxonomy 类别名称
- $slugs 要过滤的 slug/ids
- $field 选择分类项的方式。可能的值是 'term_id', 'name', 'slug' 或 'term_taxonomy_id'。默认值是 'slug'。
- $args 传递给 WP_Query 类的附加参数。
多个类别
$books->get_posts_by_terms( array $terms, $string relation = 'AND', array $args = array() );
参数
- $terms 关联数组,包含要过滤的类别。
- $relation 每个内部类别之间的逻辑关系。
- $args 传递给 WP_Query 类的附加参数。
示例
$books = new PostType( 'Book' );
$books->taxonomy( 'Genre' );
$books->taxonomy( 'Language' );
/**
* Retrieves all the "Horror" books written in "Italian" and "German"
*/
$terms = array(
'genre' => 'horror',
'language' => array( 'italian', 'german' ),
);
$books = $books->get_posts_by_terms( $terms );
设置排序
默认情况下,文章按 标题 排序:升序
$books->set_order( ORDER_BY, ORDER );
WP_Query 对象
可以通过以下方式访问最后执行的 WP_Query 对象:
$query = $books->wp_query();
管理编辑屏幕
过滤器
当您注册一个类别时,它将自动添加到管理编辑屏幕作为过滤器和一个列。
您可以使用 filters() 方法定义您想要显示的过滤器
$books->filters( array( 'genre' ) );
元框
该类有两个实用函数,可以轻松为注册的 CPT 添加元框。
添加元框
function add_meta_box( $title, $callback, $context = 'normal', $priority = 'low' );
示例
$books->add_meta_box( 'My meta box', 'my_callback_function' );
function my_callback_function( $post ) {
}
侧边栏元框
另一种方法,而不是设置 $contex = 'side',将元框注册到侧边栏是
function add_sidebar_meta_box( $title, $callback, $priority = 'low' );
示例
$books->add_sidebar_meta_box( 'My meta box', 'my_callback_function' );
function my_callback_function( $post ) {
}
列
向 CPT 列表添加自定义列
function add( $column, $label = null, $callback = null, $position = null );
示例
$books->columns()->add( 'genre', 'Genre', 'genre_callback', 2 );
function genre_callback( $key, $post_id ) {
echo $post_id;
}
从 CPT 列表中隐藏一列
/**
* Add a column to hide
*
* @param string $column the slug of the column to hdie
*/
function hide( $columns )