我正在尝试学习自定义帖子类型,我有一些问题。我正在尝试添加一个类别菜单,但由于某种原因,它似乎不起作用-我没有收到任何错误或任何东西。这是我的代码,请告诉我是否应该添加/改进某些内容:
// Create The Custom Post Type
function products_post_type(){
register_post_type( \'generic_prodcts\',
array(
\'labels\' => array(
\'name\' => __( \'Products\' ),
\'singular_name\' => __( \'Product\' ),
\'add_new_item\' => __(\'Add New Product\'),
\'edit_item\' => __(\'Edit Product\'),
\'search_items\' => __(\'Search Products\')
),
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'products\')
)
);
}
add_action( \'init\', \'products_post_type\' );
// Create The Columns
function generic_products_columns($columns){
$columns = array(
\'check\' => \'<input type="checkbox" />\',
\'title\' => __(\'Title\', \'trans\'),
\'desc\' => __(\'Description\',\'trans\'),
\'price\' => __(\'Price\', \'trans\'),
);
return $columns;
}
add_filter("manage_generic_prodcts_posts_columns", "generic_products_columns");
// Create The Column Data
function generic_products_column_data( $column, $post_id ) {
switch ( $column ) {
case "title":
$title = get_post_meta( $post_id, \'title\', true);
echo \'<a href="\' . $title . \'">\' . $title. \'</a>\';
break;
case "desc":
echo get_post_meta( $post_id, \'desc\', true);
break;
case "price":
$price = get_post_meta( $post_id, \'price\', true);
echo \'<a href="\' . $price . \'">\' . $price. \'</a>\';
break;
}
}
add_action( "manage_posts_custom_column", "generic_products_column_data");
// Make Post Type Columns Sortable
function generic_prodcts_sortable() {
return array(
\'title\' => \'Title\',
\'desc\' => \'Description\',
\'price\' => \'Price\'
);
}
add_filter( "manage_edit-generic_prodcts_sortable_columns", "generic_prodcts_sortable" );
register_taxonomy(
\'Categories\',
array(\'generic_products\'),
array(
\'hierarchical\' => true,
\'label\' => \'Categories\',
\'singular_label\' => \'Category\',
\'rewrite\' => true
)
);
还有,我如何自定义自定义帖子类型页面?我想删除文本框并添加我自己的字段。我试过用谷歌搜索它,但我不知道如何解释才能找到合适的结果。
仅供参考,我只是以下几点:
Yoast 和Wordpress Codex
最合适的回答,由SO网友:s_ha_dum 整理而成
您需要添加taxonomies
注册数组的参数:
\'taxonomies\' => array(\'category\')
即:
array(
\'labels\' => array(
\'name\' => __( \'Products\' ),
\'singular_name\' => __( \'Product\' ),
\'add_new_item\' => __(\'Add New Product\'),
\'edit_item\' => __(\'Edit Product\'),
\'search_items\' => __(\'Search Products\')
),
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'products\'),
\'taxonomies\' => array(\'category\')
)
参考:
http://codex.wordpress.org/Function_Reference/register_post_type