自定义分类链接将在帖子下打开

时间:2015-02-11 作者:Stefan Malic

我有一个自定义分类法,我想显示在自定义菜单下。我通过将自定义菜单指定为父菜单来实现这一点。结果如下:

但是,当我单击它时,CSV标记上载菜单将折叠,分类法将在Posts菜单下自动打开:

enter image description here

你知道为什么会发生这种情况,我该怎么解决吗?我已经有这个问题很长时间了,但现在它正在成为一个问题。

EDIT:用于注册分类的代码:

function csv_tags_taxonomy() {

    $labels = array(
        \'name\' => _x( \'CSV Tags\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Tag\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Tags\' ),
        \'popular_items\' => __( \'Popular Tags\' ),
        \'all_items\' => __( \'All Tags\' ),
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Tag\' ), 
        \'update_item\' => __( \'Update Tag\' ),
        \'add_new_item\' => __( \'Add New Tag\' ),
        \'new_item_name\' => __( \'New Tag Name\' ),
        \'separate_items_with_commas\' => __( \'Separate tags with commas\' ),
        \'add_or_remove_items\' => __( \'Add or remove tags\' ),
        \'choose_from_most_used\' => __( \'Choose from the most used tags\' ),
        \'menu_name\' => __( \'CSV Tags\' ),
        ); 

    register_taxonomy(\'csv_tags\',\'post\',array(
        \'hierarchical\' => false,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_admin_column\' => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'csv-tags\' ),
        ));
}
add_action(\'init\', \'csv_tags_taxonomy\');

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

尽管我的分类法已向用户注册,但我还是遇到了同样的问题。在找到答案Custom user taxonomies in WordPress by Justin Tadlock (见帖子评论)。

将其添加到代码中(用适当的名称替换\\u分类法):

function fix_user_tax_page( $parent_file = \'\' ) {
    global $pagenow;

    if ( ! empty( $_GET[ \'taxonomy\' ] ) && $_GET[ \'taxonomy\' ] == \'YOUR_TAXONOMY\' && $pagenow == \'edit-tags.php\' ) {
    $parent_file = \'users.php\';
    }

  return $parent_file;
}
编辑:好的,第一次就可以了,但我有两个自定义分类法,第二个分类法(子主题中相同functions.php文件中的所有内容)无法使用。我更改了分类名称、函数名称和全局变量名称。

有人有什么想法吗?

编辑:

首先,上面的代码缺少一行。整个事情应该是:

add_filter( \'parent_file\', \'fix_user_tax_page\' );

function fix_user_tax_page( $parent_file = \'\' ) {
        global $pagenow;

    if ( ! empty( $_GET[ \'taxonomy\' ] ) && $_GET[ \'taxonomy\' ] == \'YOUR_TAXONOMY\' && $pagenow == \'edit-tags.php\' ) {
        $parent_file = \'users.php\';
    }

    return $parent_file;
}
第二件事,我刚刚找到了将此用于多个自定义分类法的答案:删除&& $_GET[ \'taxonomy\' ] == \'YOUR_TAXONOMY\'.

SO网友:cybmeta

您的问题是,您正在将分类法和帖子类型关联起来,但您没有将其关联起来。如果您不想将分类法与帖子(标准帖子类型)关联,请不要将它们关联,这很容易。如果需要没有关联的post类型的分类,请设置$object_typenull (参见codex):

function csv_tags_taxonomy() {

    $labels = array(
        \'name\' => _x( \'CSV Tags\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Tag\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Tags\' ),
        \'popular_items\' => __( \'Popular Tags\' ),
        \'all_items\' => __( \'All Tags\' ),
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Tag\' ), 
        \'update_item\' => __( \'Update Tag\' ),
        \'add_new_item\' => __( \'Add New Tag\' ),
        \'new_item_name\' => __( \'New Tag Name\' ),
        \'separate_items_with_commas\' => __( \'Separate tags with commas\' ),
        \'add_or_remove_items\' => __( \'Add or remove tags\' ),
        \'choose_from_most_used\' => __( \'Choose from the most used tags\' ),
        \'menu_name\' => __( \'CSV Tags\' ),
        ); 

    register_taxonomy(\'csv_tags\', null, array(
        \'hierarchical\' => false,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_admin_column\' => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'csv-tags\' ),
        ));
}
add_action(\'init\', \'csv_tags_taxonomy\');

结束

相关推荐