如何在WordPress中添加自定义标签管理功能?

玖比肆 WordPress 31

请提供在WordPress中创建和管理自定义标签管理功能的方法。

回复

共1条回复 我来回复
  • 玖比肆的头像
    玖比肆 评论

    您可以使用以下步骤在WordPress中添加自定义标签管理功能:

    1. 创建自定义标签

    首先,您需要创建自定义标签。为此,您可以在functions.php文件中使用register_taxonomy函数。例如,

    ```
    // Register Custom Taxonomy
    function custom_taxonomy() {

    $labels = array(
    'name' => _x( 'Custom Tags', 'Taxonomy General Name', 'text_domain' ),
    'singular_name' => _x( 'Custom Tag', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name' => __( 'Custom Tags', 'text_domain' ),
    'all_items' => __( 'All Tags', '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 tags', 'text_domain' ),
    'popular_items' => __( 'Popular Tags', 'text_domain' ),
    'search_items' => __( 'Search Tags', 'text_domain' ),
    'not_found' => __( 'No tags 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_tag', array( 'post' ), $args );

    }
    add_action( 'init', 'custom_taxonomy', 0 );
    ```

    2. 创建标签编辑器

    接下来,您需要创建标签编辑器。为此,可以使用add_meta_box函数在后台添加新的元框。例如,

    ```
    function custom_tag_meta_box() {
    add_meta_box(
    'custom_tag_meta_box',
    __('Custom Tag', 'text_domain'),
    'custom_tag_meta_box_callback',
    'post',
    'side',
    'core'
    );
    }
    add_action('add_meta_boxes', 'custom_tag_meta_box');
    ```

    在这里,我们添加了一个名为”custom_tag_meta_box”的元框,并使用custom_tag_meta_box_callback函数来呈现其内容。

    3. 呈现标签编辑器

    现在,您需要呈现标签编辑器。例如,

    ```
    function custom_tag_meta_box_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'custom_tag_nonce' );
    $taxonomy = 'custom_tag';
    $terms = get_terms( $taxonomy, array(
    'hide_empty' => false,
    ) );
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'orderby' => 'term_id', 'order' => 'ASC' ) );
    ?>


    2023年 5月 14日 下午7:15 0条评论
微信客服
分享本页
返回顶部
玖比肆(9Bi4)专注于帮助企业实现数字化转型,提供全方位的网站建设、网络运营、网络推广、网络营销和SEO优化等一系列数字化营销服务。