创建一个名为“Gig”的自定义帖子类型,WordPress将负责UI的创建,然后只需添加一个包含所需字段(日期、地点、关于)的元数据库。
以下是一个简单的分步指南:
First: Register Your Gig Post Type
//register your custom post type gig
add_action( \'init\', \'register_cpt_gig\' );
function register_cpt_gig() {
$labels = array(
\'name\' => _x( \'gigs\', \'gig\' ),
\'singular_name\' => _x( \'gig\', \'gig\' ),
\'add_new\' => _x( \'Add New\', \'gig\' ),
\'add_new_item\' => _x( \'Add New gig\', \'gig\' ),
\'edit_item\' => _x( \'Edit gig\', \'gig\' ),
\'new_item\' => _x( \'New gig\', \'gig\' ),
\'view_item\' => _x( \'View gig\', \'gig\' ),
\'search_items\' => _x( \'Search gigs\', \'gig\' ),
\'not_found\' => _x( \'No gigs found\', \'gig\' ),
\'not_found_in_trash\' => _x( \'No gigs found in Trash\', \'gig\' ),
\'parent_item_colon\' => _x( \'Parent gig:\', \'gig\' ),
\'menu_name\' => _x( \'Gigs\', \'gig\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'description\' => \'just a simple "where, when, with who" kind of thing\',
\'supports\' => array( \'title\', \'custom-fields\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_icon\' => \'http://i.imgur.com/4nTMD.png\',
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\'=> \'post\'
);
register_post_type( \'gig\', $args );
}
这将为您提供一个新的帖子类型和管理UI,用于创建您的Gig:
Next you need to create a Metabox (避免使用常规自定义字段)
网上有一百万个关于如何创建和添加元数据库的教程,所以我不打算深入讨论,但我将向您展示一种快速简便的方法。Download this class 一旦你把这个放在你的主题中,Metabox的创建应该是这样的:
if( is_admin() ) {
//include the main class file
require_once( "meta-box-class/my-meta-box-class.php" );
$prefix = \'_gigs\';
//configure your meta box
$config = array(
\'id\' => \'gigs-info\',
\'title\' => \'Gig Info\',
\'pages\' => array(\'gig\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(),
\'local_images\' => false,
\'use_with_theme\' => false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
);
$my_meta = new AT_Meta_Box( $config );
//Add fields to your meta box
$my_meta->addText( $prefix . \'where\', array( \'name\'=> \'Where is the Gig \' ) );
$my_meta->addDate( $prefix . \'when\', array( \'name\'=> \'When is the Gig \' ) );
$my_meta->addTime( $prefix . \'start_time\', array( \'name\'=> \'When Does it Start \' ) );
$my_meta->addTime( $prefix . \'end_time\', array( \'name\'=> \'When Does it End \' ) );
$my_meta->addText( $prefix . \'with_who\', array( \'name\'=> \'With Who is the Gig \' ) );
$my_meta->addTextarea( $prefix . \'words\', array( \'name\'=> \'A few words on the gig \' ) );
$my_meta->Finish();
}
您最终会得到类似的结果:
下面是另一幅展示奇特计时器的图片:
有关可以使用并添加到元数据库的字段类型的详细信息read this.
就是这样!总而言之,我将此作为一个插件发布以进行测试,但请确保首先下载Metabox类,这样它才能工作。
<?php
/*
Plugin Name: wp-gigs
Plugin URI: http://en.bainternet.info
Description: create list of gigs.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/
//register your custom post type gig
add_action( \'init\', \'register_cpt_gig\' );
function register_cpt_gig() {
$labels = array(
\'name\' => _x( \'gigs\', \'gig\' ),
\'singular_name\' => _x( \'gig\', \'gig\' ),
\'add_new\' => _x( \'Add New\', \'gig\' ),
\'add_new_item\' => _x( \'Add New gig\', \'gig\' ),
\'edit_item\' => _x( \'Edit gig\', \'gig\' ),
\'new_item\' => _x( \'New gig\', \'gig\' ),
\'view_item\' => _x( \'View gig\', \'gig\' ),
\'search_items\' => _x( \'Search gigs\', \'gig\' ),
\'not_found\' => _x( \'No gigs found\', \'gig\' ),
\'not_found_in_trash\' => _x( \'No gigs found in Trash\', \'gig\' ),
\'parent_item_colon\' => _x( \'Parent gig:\', \'gig\' ),
\'menu_name\' => _x( \'Gigs\', \'gig\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'description\' => \'just a simple "where, when, with who" kind of thing\',
\'supports\' => array( \'title\', \'custom-fields\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_icon\' => \'http://i.imgur.com/4nTMD.png\',
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\'
);
register_post_type( \'gig\', $args );
}
if( is_admin() ) {
//include the main class file
require_once( "meta-box-class/my-meta-box-class.php" );
$prefix = \'_gigs\';
//configure your meta box
$config = array(
\'id\' => \'gigs-info\',
\'title\' => \'Gig Info\',
\'pages\' => array( \'gig\' ),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(),
\'local_images\' => false,
\'use_with_theme\'=> false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
);
$my_meta = new AT_Meta_Box( $config );
//Add fields to your meta box
$my_meta->addText( $prefix . \'where\', array( \'name\'=> \'Where is the Gig \' ) );
$my_meta->addDate( $prefix . \'when\', array( \'name\'=> \'When is the Gig \' ) );
$my_meta->addTime( $prefix . \'start_time\', array( \'name\'=> \'When Does it Start \' ) );
$my_meta->addTime( $prefix . \'end_time\', array( \'name\'=> \'When Does it End \' ) );
$my_meta->addText( $prefix . \'with_who\', array( \'name\'=> \'With Who is the Gig \' ) );
$my_meta->addTextarea( $prefix . \'words\', array( \'name\'=> \'A few words on the gig \' ) );
$my_meta->Finish();
}