內容簡介
這是一個提供方便的類別和方法集,能讓建立帶有自己 metabox 的新自訂文章型別變得容易。
一般的做法是定義一個函式,然後將其連接到 register_post_types 動作。這個函式定義了您要建立的自訂文章型別的類別,該類別繼承自 GO_Cpt 類別。這個設計模式是現代小工具 API 所建立的 (在 WP 2.8 中引入) https://codex.wordpress.org/Widgets_API#Developing_Widgets 。
結果就是有了一個新的自訂文章型別,並且更容易創建和更新與該文章型別相關的自訂 meta。您還可以使用方便的物件來存取每個文章所關聯的 meta。
function my_customposttype() { class My_CustomPostType extends GO_Cpt { function __construct() { // 用自訂文章型別的名稱和定義參數
// 呼叫父類別的建構元 parent::__construct( $post_type_name , $post_type_definition ); } function metabox( $post , $meta ) { // 在這裡列印出表單 //
// 這個方法是可選的,只有在您要建立 metabox 的時候
// 才使用它 } function update( $new_meta , $old_meta ) { // 執行 metadata 的清洗與驗證 // 然後返回 $meta,使其可以被儲存 // // 如果有 metabox() 方法,這個方法就是必要的 } } global $my_customposttype; $my_customposttype = new My_CustomPostType; } // 把這個函式連接到 register_post_types 動作 add_action( 'register_post_types' , 'my_customposttype' );
背景:在函式內定義文章型別的類別,避免了載入順序的問題,也避免了因為嘗試來擴充可能還不存在的類別而導致的致命錯誤。PHP 多麼棒啊!
請見附帶的 hello world 範例:https://plugins.trac.wordpress.org/browser/go-cptfactory/trunk/example-helloworld-cpt.php
若要存取文章的 meta,可以呼叫 $my_customposttype->get_meta( $post_id );
您可以輕鬆地添加一個方法和連接到 the_content 或 the_excerpt 的過濾器,以插入可能已輸入到文章 meta 中的自訂值。只需在 __construct() 中添加 add_filter( 'the_content' , array( $this , 'the_content' ));,然後定義 My_CustomPostType 類別中的另一個方法,例如:
function the_content( $content ) {
$meta_print = print_r( $this->get_meta( get_the_ID() ) , TRUE );
return $meta_print . $content
}
`
外掛標籤
開發者團隊
② 後台搜尋「GigaOM Custom Post Type Factory」→ 直接安裝(推薦)
原文外掛簡介
This is a convenience class and collection of methods to make creating new custom post types with their own metaboxes easy.
The general idea is to define a function that’s hooked to the register_post_types action. The function defines a class for your post type that extends the GO_Cpt class. This follows a design pattern established by the modern widgets API (as introduced in WP 2.8) https://codex.wordpress.org/Widgets_API#Developing_Widgets .
The result is a new custom post type and much easier creation and updating of custom meta associated with that post type. You’ll also have a convenient object you can use to access the metadata associated with each post.
function my_customposttype() { class My_CustomPostType extends GO_Cpt { function __construct() { // execute the parent constructor with the name
// and definition for the custom post type parent::__construct( $post_type_name , $post_type_definition ); } function metabox( $post , $meta ) { // print out the form here //
// this method is optional, only use it if you want to
// add a metabox to your custom post type } function update( $new_meta , $old_meta ) { // sanitize and validate the metadata // then return $meta so it can be saved // // this method is required if a
// metabox() method is included } } global $my_customposttype; $my_customposttype = new My_CustomPostType; } // hook that function to the register_post_types action add_action( 'register_post_types' , 'my_customposttype' );
Backstory: defining the post type class inside the function avoids issues with loading order and fatal errors resulting from attempting to extend a class that might not exist yet. Isn’t PHP awesome?
See the included hello world example: https://plugins.trac.wordpress.org/browser/go-cptfactory/trunk/example-helloworld-cpt.php
To access the meta associated with a post, you can call $my_customposttype->get_meta( $post_id );
You can easily add a method and hook to filter the_content or the_excerpt to insert custom values that may have been entered in the post meta. Simply add add_filter( 'the_content' , array( $this , 'the_content' )); in the __construct() and then define another method in the My_CustomPostType class such as:`
function the_content( $content ) {
$meta_print = print_r( $this->get_meta( get_the_ID() ) , TRUE );
return $meta_print . $content
}
`
