93digital/custom-post-type

此包已废弃,不再维护。未建议替代包。

93digital提供的自定义文章类型实用函数

dev-master 2021-07-29 14:37 UTC

This package is not auto-updated.

Last update: 2021-08-07 12:39:37 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'参数

默认情况下,新的自定义文章类型设置以下参数

  • 标题
  • 编辑器
  • 缩略图

要覆盖/更改,请传递一个作为第四个参数的数组,例如

$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 Plura] 设置

该页面可用于添加字段,并将显示在 选项页面 选项中。

要检索存储在自定义页面中的信息,请使用

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 (int) 每页显示的文章数。默认 = 0(不设置 WP_Query 参数)
  • $args (array) 要传递给 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 要筛选的 slugs 列表。
示例

假设我们为我们的cpt注册了分类法 genre

$books = new PostType( 'Book' );
$books->taxonomy( 'Genre' );

现在我们可以轻松地过滤我们的帖子

$book_cpt->get_posts_by_genre( 'horror' );

如果需要,我们还可以传递一个包含多个值的slugs数组进行过滤

单个分类法

通过单个分类法过滤的另一种方法是

$books->get_posts_by_term( string $taxonomy, string|int|array $slugs, string $field = 'slug', array $args = array() );
参数
  • $taxonomy 分类法名称
  • $slugs 要过滤的slugs/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 );

设置排序

默认情况下,帖子按 标题 升序排序: ASC

$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 )