couleurcitron / wp-utils
WordPress 网站工具
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- giggsey/libphonenumber-for-php: ^8.10
- guzzlehttp/guzzle: ^6.3
- illuminate/container: ^7.30
- illuminate/support: ^7.30
- illuminate/validation: ^7.30
- symfony/http-foundation: ^5.1
Requires (Dev)
- symfony/var-dumper: ^5.1
README
安装
composer require couleurcitron/wp-utils
使用
文章类型
注册自定义文章类型。
扩展
CouleurCitron\WPUtils\PostType抽象类,并提供与register_post_type()函数相同的文章类型配置。use CouleurCitron\WPUtils\PostType; class MyCustomPostType extends PostType { /** * Post type configuration * * @see https://developer.wordpress.org/reference/functions/register_post_type/ * * @return array */ protected static function config() { return [ // ]; } }在您的
functions.php中注册文章类型。无需在钩子中执行。MyCustomPostType::register();
分类法
注册自定义分类法。
扩展
CouleurCitron\WPUtils\Taxonomy抽象类,并提供与register_taxonomy()函数相同的分类法配置。还可以为此分类法提供一个(字符串)或多个(字符串数组)的对象类型。
可以是文章类型名称(对于使用此库注册的文章类型,通过MyCustomPostType::name()可用)或类的完全限定名(MyCustomPostType::class)。use CouleurCitron\WPUtils\Taxonomy; class MyCustomTaxonomy extends Taxonomy { /** * Set object type(s) for the taxonomy * * @return string|array */ protected static function objectType() { // } /** * Taxonomy configuration * * @see https://developer.wordpress.org/reference/functions/register_taxonomy/ * * @return array */ protected static function config() { return [ // ]; } }在您的
functions.php中注册分类法。无需在钩子中执行。MyCustomTaxonomy::register();
列
在管理文章列表中添加列。用于显示元值。
use CouleurCitron\WPUtils\Column;
Column::make( 'custom_field', 'Custom Field' )
->label( 'Custom Field' ) // Optional if the ::make() second argument is provided
->order( 1 ) // Position of the column from the left, first column is at position 0
->sortable() // Optional, the sort() method automatically turns on sortability
->sort( function ( WP_Query $query ) { // Customize the WP_Query when the column is sortable
$query->set( 'meta_key', 'custom_field' );
$query->set( 'orderby', 'meta_value' );
} )
->render( function ( int $post_id ) { // Display the column value
echo get_post_meta( $post_id, 'custom_field', true );
} )
->register( MyCustomPostType::name() /* or 'post' */ ); // Register the column to show on the given post type
对于使用此库注册的文章类型,您可以通过覆盖 columns() 方法在类中声明列。
use CouleurCitron\WPUtils\Column;
use CouleurCitron\WPUtils\PostType;
class MyCustomPostType extends PostType {
/**
* Post type configuration
*
* @see https://developer.wordpress.org/reference/functions/register_post_type/
*
* @return array
*/
protected static function config() {
return [
//
];
}
/**
* @return \CouleurCitron\WPUtils\Column[]
*/
protected static function columns() {
return [
// No need to call register() on the column, it will be automatically registered for this post type
Column::make( 'custom_field', 'Custom Field' )
->label( 'Custom Field' )
->order( 1 )
->sortable()
->sort( function ( WP_Query $query ) {
$query->set( 'meta_key', 'custom_field' );
$query->set( 'orderby', 'meta_value' );
} )
->render( function ( int $post_id ) {
echo get_post_meta( $post_id, 'custom_field', true );
} ),
];
}
}
方法和属性
返回多个项的方法将返回一个 Illuminate\Support\Collection(文档)。
CouleurCitron\WPUtils\PostType
PostType::$posts_per_page;
如果不为 null,则设置在文章类型存档页上显示的文章数量。否则,将默认为全局 WordPress 设置。PostType::$translatable = false;
为此文章类型启用翻译。(仅与 Polylang 兼容)。PostType::register(): void;
注册文章类型。无需在钩子中调用。PostType::name(): string;
获取文章类型名称(例如'my-custom-post-type')。PostType::all( array $args = [] ): Illuminate\Support\Collection;
对此文章类型执行查询。默认返回所有文章。PostType::insert( array $args ): WP_Post;
为此文章类型插入一个新文章。$args与wp_insert_post()相同。
CouleurCitron\WPUtils\Taxonomy
PostType::$posts_per_page;
If not null, set the number of posts to display on the term page. Otherwise, it will default to the global WordPress setting.PostType::$translatable = false;
Enable translation for this taxonomy. (Only compatible with Polylang).Taxonomy::register(): void;
注册分类法。无需在钩子中调用。Taxonomy::name(): string;
获取分类法名称(例如'my-custom-taxonomy')。Taxonomy::all( array $args = [] ): Illuminate\Support\Collection;
对此分类法执行分类查询。默认返回所有术语。Taxonomy::find( int $id ): WP_Term;
通过 id 查找此分类法的一个术语。Taxonomy::fromPost( int $id ): Illuminate\Support\Collection;
为给定的文章查找此分类法的所有术语。Taxonomy::insert( string $term, array $args = [] ): WP_POST;
为此分类法插入一个新的术语。参数与wp_insert_term()相同。