Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

PostType

Aleksandr Strikha edited this page Oct 8, 2015 · 7 revisions

namespace: WPKit\PostType

Wrapper class for register_post_type.
Register and configure a post type.

Constructor

string $key - unique key;
string $singular_name - post type title (singular);
array $labels (optional) - array of post type labels;

Example:

$product_post_type = new PostType( 'product', __( 'Product' ) );

This example creates a post type named "Product". You do not need to describe all labels of post type, the WPKit will do it for you. But if, for any reason, you need to define certain labels, then you can easily do it:

$product_post_type = new PostType( 'product', __( 'Product' ), [
   'menu_name' => __( 'Shop' ), 
   'add_new'   => __( 'Add New Product' ),  
] );

Methods:

set_supports( array $supports )

Registers support of certain feature(s) for a post type. See add_post_type_support.
Default: ['title', 'editor', 'thumbnail']

set_menu_icon( $icon )

Set icon to be used for this menu or the name of the icon from the iconfont. Icon can be defined in 3 ways:

  1. Icon url, example "http://site.com/icons/product.png"
  2. Base64 encoded content of SVG icon, example "data:image/svg+xml;base64,PD94bWwgbj..."
  3. Icon name, example 'dashicons-cart'. See dashicons

add_meta_box( MetaBox $meta_box )

Attach MetaBox to post type.

add_taxonomy( Taxonomy $taxonomy )

Attach Taxonomy to post type.

add_column( $title, $function, $sortable = false, $position = -1 )

Add custom column to posts listing table.

Example:

$product_post_type->add_column( 'Price', function( $column ) {
   echo get_post_meta( get_the_ID(), 'price', true );
} );

add_column_thumbnail( $title = 'Image', $position = 1 )

Add thumbnail column to posts listing table.

set_pluralize( $pluralize )

Set automatic pluralization.
Default: true