WordPress:如何在安装WordPress主题期间自动添加一些标签?

时间:2013-07-03 19:23:34

标签: wordpress themes

如何在安装WordPress主题期间自动添加一些标签?

2 个答案:

答案 0 :(得分:0)

在主题register_taxonomy()中添加function.php。我正在复制粘贴from WP Codex

// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
    'name'                       => _x( 'Writers', 'taxonomy general name' ),
    'singular_name'              => _x( 'Writer', 'taxonomy singular name' ),
    'search_items'               => __( 'Search Writers' ),
    'popular_items'              => __( 'Popular Writers' ),
    'all_items'                  => __( 'All Writers' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit Writer' ),
    'update_item'                => __( 'Update Writer' ),
    'add_new_item'               => __( 'Add New Writer' ),
    'new_item_name'              => __( 'New Writer Name' ),
    'separate_items_with_commas' => __( 'Separate writers with commas' ),
    'add_or_remove_items'        => __( 'Add or remove writers' ),
    'choose_from_most_used'      => __( 'Choose from the most used writers' ),
    'not_found'                  => __( 'No writers found.' ),
    'menu_name'                  => __( 'Writers' ),
);

$args = 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' => 'writer' ),
);

register_taxonomy( 'writer', 'book', $args );

这不会增加安装税,但首次加载时;无论如何,我认为这应该是方式(如果你安装它,你将加载它)。

答案 1 :(得分:0)

您可以使用wp_insert_term以编程方式添加字词。请参阅function reference。还有wp_update_term。这些函数适用于所有类型的术语 - 类别和标签以及自定义分类。

您需要将这些函数放在functions.php文件中的函数中,然后将其添加到init操作中。

我没有测试过这段代码,但它应该可以运行:

function add_tags() {
    wp_insert_term(
        'Butts', // the tag/term name
        'tags',  // the taxonomy name
        array(
            'description' => 'Posts about Butts',
            'slug' => 'butts'
    ));
add_action( 'init', 'add_tags' );