首页 > 解决方案 > 我可以为 wordpress 创建第二个自定义标签系统吗?

问题描述

我需要在 Wordpress 中有一个主要和次要标记系统,但我发现很难创建第二个自定义标记系统,而且似乎没有太多关于这方面的教程。

有谁知道实现这一目标的方法?

感谢您的时间和建议!

我需要一些不同于自定义分类的东西,我需要它是标签。

标签: wordpresswordpress-theming

解决方案


如果我理解正确,您可以根据需要创建任意数量的自定义标签。这是创建两个自定义标签的示例:

// Register Custom Taxonomy
function custom_tags1() {

$labels = array(
    'name'                       => _x( 'Custom Tags 1', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Custom Tags 1', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Custom Tags 1', 'text_domain' ),
    'all_items'                  => __( 'All Custom Tags 1', 'text_domain' ),
    'parent_item'                => __( 'Parent Tag', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Tag:', 'text_domain' ),
    'new_item_name'              => __( 'New Tag Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Tag', 'text_domain' ),
    'edit_item'                  => __( 'Edit Tag', 'text_domain' ),
    'update_item'                => __( 'Update Tag', 'text_domain' ),
    'view_item'                  => __( 'View Tag', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Tags with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Tags', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Tags', 'text_domain' ),
    'search_items'               => __( 'Search Tags', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No Tags', 'text_domain' ),
    'items_list'                 => __( 'Tags list', 'text_domain' ),
    'items_list_navigation'      => __( 'Tags list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'custom_tags_1', array( 'post' ), $args );

}
add_action( 'init', 'custom_tags1', 0 );

// Register Custom Taxonomy
function custom_tags2() {

$labels = array(
    'name'                       => _x( 'Custom Tags 2', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Custom Tags 2', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Custom Tags 2', 'text_domain' ),
    'all_items'                  => __( 'All Custom Tags 2', 'text_domain' ),
    'parent_item'                => __( 'Parent Tag', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Tag:', 'text_domain' ),
    'new_item_name'              => __( 'New Tag Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Tag', 'text_domain' ),
    'edit_item'                  => __( 'Edit Tag', 'text_domain' ),
    'update_item'                => __( 'Update Tag', 'text_domain' ),
    'view_item'                  => __( 'View Tag', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Tags with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Tags', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Tags', 'text_domain' ),
    'search_items'               => __( 'Search Tags', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No Tags', 'text_domain' ),
    'items_list'                 => __( 'Tags list', 'text_domain' ),
    'items_list_navigation'      => __( 'Tags list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'custom_tags_2', array( 'post' ), $args );

}
add_action( 'init', 'custom_tags2', 0 );

推荐阅读