首页 文章

如何以编程方式创建Wordpress标签?

提问于
浏览
2

以下代码段以编程方式添加Wordpress类别 . 我的问题是你如何以编程方式添加标签?

//Define the category
$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'category_parent' => '');

// Create the category
$my_cat_id = wp_insert_category($my_cat);

在这个问题中,我所说的是以编程方式将标签添加到数据库中 .

说,我有1000个标签添加到全新安装 . 而且我不想通过常规管理面板手动逐个添加标签 . 我正在寻找一个程序化的解决方案 . 我发布的代码片段负责添加猫...感谢特定的wp函数wp_insert_category ....虽然没有函数叫做wp_insert_tag ...

然而,看着这个代码,我看到了wp_insert_term函数,它可能很适合做这项工作 - 看来 .

1 回答

  • 7

    使用wp_insert_term()添加类别,标签和其他分类,因为 wp_insert_category() 会触发PHP错误"Undefined function" .

    <?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>
    

    $term 是要添加或更新的术语 .

    如果 $taxonomy 是标记,则将 $taxonomy 的值更改为 post_tag ,如果是类别,则将 category 更改为 category .

    $args 数组中,您可以指定插入的术语(标记,类别等)的值

    Example:

    wp_insert_term(
      'Apple', // the term 
      'product', // the taxonomy
      array(
        'description'=> 'A yummy apple.',
        'slug' => 'apple',
        'parent'=> $parent_term['term_id']  // get numeric term id
      )
    );
    

相关问题