创建自定义帖子类型
function create_product()
{
$labels = array(
\'name\' => _x( \'Product\', \'post type general name\', \'stacy\' ),
\'singular_name\' => _x( \'product\', \'post type singular name\', \'stacy\' ),
\'menu_name\' => _x( \'Products\', \'admin menu\', \'stacy\' ),
\'name_admin_bar\' => _x( \'Product\', \'add new on admin bar\', \'stacy\' ),
\'add_new\' => _x( \'Add New\', \'product\', \'stacy\' ),
\'add_new_item\' => __( \'Add New Product\', \'stacy\' ),
\'new_item\' => __( \'New Product\', \'stacy\' ),
\'edit_item\' => __( \'Edit Product\', \'stacy\' ),
\'view_item\' => __( \'View Product\', \'stacy\' ),
\'all_items\' => __( \'All Product\', \'stacy\' ),
\'search_items\' => __( \'Search Product\', \'stacy\' ),
\'not_found\' => __( \'No Product found.\', \'stacy\' ),
\'not_found_in_trash\' => __( \'No Product found in Trash.\', \'stacy\' )
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Description.\', \'Add New Product on stacy\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'product\' ),
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => 100,
\'menu_icon\' =>\'dashicons-cart\',
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\',\'comments\',\'capabilities\' ),
\'taxonomies\' => array(\'product_category\',\'product_tag\')
);
register_post_type( \'product\', $args );
}
add_action( \'init\', \'create_product\' );
创建自定义元框并在编辑帖子时显示元值
function add_product_details_meta_box()
{
global $wpdb;
global $post;
$custom = get_post_custom( $post->ID );
<p>
<label>Short Description:</label><br />
<textarea rows="5" name="short_description" class="width99"><?= @$custom["short_description"][0] ?></textarea>
</p>
<p>
<label>Price:</label><br />
<input type="text" name="price" value="<?= @$custom["price"][0] ?>" class="width99" />
</p>
<p>
<label>Dimensions (in):</label><br />
<input type="text" name="length" value="<?= @$custom["length"][0] ?>" class="s" placeholder="Length"/>
</p>
<p>
<label>Shipping Lead Days:</label><br />
<input type="text" name="ship_lead_days" value="<?= @$custom["product_ship_lead_days"][0] ?>" class="s" placeholder="Shipping Lead Days"/>
</p>
<p>
<label>Commision:</label><br />
<input type="text" name="commision_broker" value="<?= @$custom["commision_broker"][0] ?>" class="s" placeholder="Enter Your Commision Here"/>
</p>
}
add_action( \'admin_init\', \'add_product_meta_boxes\' );
更新Post Meta
function save_product_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "short_description", @$_POST["short_description"]);
update_post_meta($post->ID, "price", @$_POST["price"]);
update_post_meta($post->ID, "length", @$_POST["length"]);
update_post_meta($post->ID,\'product_ship_lead_days\',@$_POST[\'ship_lead_days\']);
update_post_meta($post->ID,\'commision_broker\',@$_POST[\'commision_broker\']);
}
}
add_action( \'save_post\', \'save_product_custom_fields\' );