我的帖子类型是7(数到7!)分类标记框。默认情况下,显示这些元盒的唯一选项是在右侧堆叠-7个小元盒,或将这些标记元盒转换为长元盒,并将其堆叠在主编辑器下方。
我更愿意做的是将所有的元盒放入编辑器下方的一个大元盒中,将它们堆叠在3x3网格中(实际上是每行3个,剩余1个)。
我的问题是,当WordPress创建元盒时,我是否可以控制它的显示方式或显示位置?是否可以以某种方式对这些代谢组进行分组?
Post Type / Taxonomy Registers
// Products Custom Post Type
register_post_type( \'cpt_products\', array(
\'labels\' => array(
\'name\' => __( \'Products\' ),
\'singular_name\' => __( \'Product\' ),
\'all_items\' => __( \'View Products\' ),
\'add_new\' => __( \'New Product\' ),
\'add_new_item\' => __( \'New Product\' ),
\'edit_item\' => __( \'Edit Product\' ),
\'view_item\' => __( \'View Product\' ),
\'search_items\' => __( \'Search Products\' ),
\'no_found\' => __( \'No Products Found\' ),
\'not_found_in_trash\' => __( \'No Products in Trash\' )
),
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => false,
\'show_in_nav_menus\' => false,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 20,
\'menu_icon\' => \'dashicons-cart\',
\'has_archive\' => \'products\',
\'rewrite\' => array( \'slug\' => \'product\', \'with_front\' => false ),
\'supports\' => array( \'title\',\'editor\',\'thumbnail\',\'page-attributes\' )
));
// Testing Taxonomy for Products
register_taxonomy( \'tax_testing\', \'cpt_products\', array(
\'labels\' => array(
\'name\' => __( \'Test\' ),
\'singular_name\' => __( \'Test\' ),
\'menu_name\' => __( \'View Test\' ),
\'all_items\' => __( \'All Test\' ),
\'edit_item\' => __( \'Edit Test\' ),
\'view_item\' => __( \'View Test\' ),
\'update_item\' => __( \'Update Test\' ),
\'add_new_item\' => __( \'New Test\' ),
\'new_item_name\' => __( \'Rename Test\' ),
\'parent_item\' => __( \'Parent Test\' ),
\'parent_item_colon\' => __( \'Parent Test:\' ),
\'search_items\' => __( \'Search Test\' ),
\'popular_items\' => __( \'Popular Test\' ),
\'seperate_items_with_commas\'=> __( \'Separate Test with commas\' ),
\'add_or_remove_items\' => __( \'Add or remove Test\' ),
\'choose_from_most_used\' => __( \'choose from most used Test\' ),
\'not_found\' => __( \'No Test found.\' )
),
\'show_ui\' => true,
\'show_admin_column\' => true,
\'sort\' => true,
\'rewrite\' => array( \'slug\' => \'products/test\', \'with_front\' => false )
));
SO网友:s_ha_dum
将分类注册到\'show_ui\' => false,
然后添加一个元框来管理它们。
function create_book_tax() {
register_taxonomy(
\'genre\',
\'book\',
array(
\'label\' => __( \'Genre\' ),
\'rewrite\' => array( \'slug\' => \'genre\' ),
\'hierarchical\' => true,
\'show_ui\' => false,
)
);
}
或者从侧面板上取下方框,将其放在主编辑器下方(使用文档中的post type和tax表单)。
add_action(
\'add_meta_boxes_book\',
function () {
remove_meta_box( \'genrediv\', \'book\', \'side\' );
$tax_name = \'genre\';
$taxonomy = get_taxonomy( $tax_name );
$label = $taxonomy->labels->name;
$tax_meta_box_id = $tax_name . \'div\';
add_meta_box(
$tax_meta_box_id,
$label,
$taxonomy->meta_box_cb
);
}
);
第三种选择是创建您自己的一组元框容器,并将您的框添加到这些容器中。
add_action(
\'add_meta_boxes_book\',
function () {
remove_meta_box( \'genrediv\', \'book\', \'side\' );
}
);
add_action(
\'dbx_post_sidebar\',
function ($post) {
$tax_name = \'genre\';
$taxonomy = get_taxonomy( $tax_name );
$label = $taxonomy->labels->name;
$tax_meta_box_id = $tax_name . \'div\';
add_meta_box(
$tax_meta_box_id,
$label,
$taxonomy->meta_box_cb,
\'book\',
\'mycol1\'
);
echo \'my boxes\'; //debug
do_meta_boxes(\'book\', \'mycol1\', $post);
do_meta_boxes(\'book\', \'mycol2\', $post);
do_meta_boxes(\'book\', \'mycol3\', $post);
echo \'end my boxes\'; //debug
}
);
如果查看源代码,您将看到您创建的容器被
div
s与
mycol*-sortables
id
. 您应该能够使用它来创建所需的列。
SO网友:gmazzap
此答案中的编辑代码对自定义帖子类型的非层次分类法有问题。
对于核心帖子类型(帖子、页面),它适用于层次和非层次分类法。对于CPT,它只适用于层次分类法。
这个问题似乎和javascript有关,我真的不想挖掘WP javascript代码来解决它。
这个答案使用了相同的方法@s_ha_dum 但使用核心功能get_taxonomies
, post_categories_meta_box
和post_tags_meta_box
要输出单个元盒,core使用的相同功能可以确保与任何第三方代码的兼容性,并避免额外的工作,例如检查用户是否有能力添加术语等。
作为奖励,它可以动态地为所有分类法工作,而无需任何额外的工作。
add_action( \'add_meta_boxes\', \'my_taxonomies_meta_box\', 10, 2 );
function my_taxonomies_meta_box( $post_type, $post ) {
// all public taxonomies for current post type
$taxs = get_taxonomies(
array( \'object_type\' => array( $post_type ), \'show_ui\' => true )
);
$output = \'<div>\';
foreach ( $taxs as $tax ) {
$i = ! isset($i) ? 1 : $i + 1;
$cb = \'post_categories_meta_box\';
$id = "{$tax}div";
// set callback and id for non-hierarchical taxonomies
if ( ! is_taxonomy_hierarchical( $tax ) ) {
$cb = \'post_tags_meta_box\';
$id = "tagsdiv-{$tax}";
}
remove_meta_box( $id, $post_type, \'side\' ); // remove core metabox
$tax_obj = get_taxonomy( $tax );
$args = array(
\'args\' => array( \'taxonomy\' => $tax ), \'title\' => $tax_obj->labels->name
);
// add a 1/3 wide div with tax metabox
$format = \'<div id="%s" class="postbox" style="%s">\';
$output .= sprintf( $format, $id, \'padding:8px;width:30%;margin:1%;float:left;\' );
$output .= \'<h3>\' . $tax_obj->labels->name . \'</h3>\';
ob_start();
call_user_func( $cb, $post, $args );
$output .= ob_get_clean(). \'</div>\';
if ( ( $i !== 0 && $i%3 === 0 ) || $i === count( $taxs ) ) {
$output .= \'<div style="width:100%;clear:both;"></div>\';
}
} // end foreach
$output .= \'</div><div style="width:100%;clear:both;"></div>\';
// add a callback that will output all the markup generated
add_meta_box( \'all_taxonomies\', __(\'Taxonomies\'), function() use( $output ) {
echo $output;
}, $post_type, \'normal\', \'high\' );
} // end function