当标签显示为复选框时,在更新后将标签保存为标签名称和标签ID

时间:2016-07-24 作者:tiadotdev

我创建了一个自定义的非层次分类法,并将其更改为显示为复选框,而不是标记云。然而,当我在帖子编辑器中,选择一个可用的标签,然后更新帖子时,会发生一些奇怪的事情。标记现在保存为标记ID号,标记ID号在标记元框中显示为选中的复选框。

以下是我的分类代码:

add_action( \'init\', \'create_cuturalmaptags_taxonomy\', 0 );

function create_cuturalmaptags_taxonomy() {

  $labels = array(
    \'name\' => _x( \'Tags\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Tag\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Tags\' ),
    \'all_items\' => __( \'All Tags\' ),
    \'parent_item\' => __( \'Parent Tag\' ),
    \'parent_item_colon\' => __( \'Parent Tag:\' ),
    \'edit_item\' => __( \'Edit Tag\' ), 
    \'update_item\' => __( \'Update Tag\' ),
    \'add_new_item\' => __( \'Add New Tag\' ),
    \'new_item_name\' => __( \'New Tag Name\' ),
    \'menu_name\' => __( \'Tags\' ),
  );    

  register_taxonomy(\'cuturalmap-tags\', array(\'post\'), array(
    \'hierarchical\' => false,
    \'meta_box_cb\' => \'post_categories_meta_box\',
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'cuturalmap-tags\' ),
  ));

}
下面是正在发生的事情的示例。当我去编辑帖子并选择我想要的标签时,它看起来很正常。enter image description here

但一旦我更新了帖子,标签如下所示:enter image description here

我在标签管理器中确认,该编号是我创建的标签的ID。发生这种情况的原因以及我如何解决?

这是我的CPT代码,其中调用了“culturalmap标记”。

// Cultural Mapping Custom Post Type
function culturalmap_post_type() {

    $labels = array(
        \'name\'                => _x( \'Cultural Map\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Cultural Map\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Cultural Mapping\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Cultural Map:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Cultural Maps\', \'text_domain\' ),
        \'view_item\'           => __( \'View Post\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Cultural Map\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Post\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Cultural Map\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Cultural Maps\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'Cultural Map\', \'text_domain\' ),
        \'description\'         => __( \'Post Type Description\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'custom-fields\' ),
        \'taxonomies\'          => array( \'cultural-mapping\', \'cuturalmap-tags\' ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'menu_icon\'           => \'dashicons-admin-site\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
        \'rewrite\'             => array(\'slug\' => \'cultural-map\'),

    );
    register_post_type( \'culturalmap\', $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'culturalmap_post_type\', 0 );

1 个回复
最合适的回答,由SO网友:Anton Lukin 整理而成

也面临着这个问题。

您可以通过添加函数来解决此问题。php筛选代码:

add_action( \'admin_init\', function() {
    if( isset( $_POST[\'tax_input\'] ) && is_array( $_POST[\'tax_input\'] ) ) {
        $new_tax_input = array();
        foreach( $_POST[\'tax_input\'] as $tax => $terms) {
            if( is_array( $terms ) ) {
              $taxonomy = get_taxonomy( $tax );
              if( !$taxonomy->hierarchical ) {
                  $terms = array_map( \'intval\', array_filter( $terms ) );
              }
            }
            $new_tax_input[$tax] = $terms;
        }
        $_POST[\'tax_input\'] = $new_tax_input;
    }
});
分叉代码段中的完整代码https://gist.github.com/antonlukin/da2b8107c67e677928a87398d89ca202

相关推荐

404 - Taxonomy Archive Page

我正在创建一个Wordpress自定义帖子类型,包括一个分类法,在安装之后,所有的永久链接都被刷新了,但是分类法归档页面不断给我404。CPT归档页面工作得非常好。要创建的代码:add_action( \'init\', \'register_cpt_post_type\' ); function register_cpt_post_type() { register_post_type( \'offers\', array(