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

问题描述 投票:2回答:1

以下代码段以编程方式添加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函数,它可能很适合做这项工作 - 看来。

wordpress
1个回答
7
投票

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

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

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

$taxonomy的值更改为post_tag(如果它是标记)和category(如果它是类别)。

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

例:

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
  )
);
© www.soinside.com 2019 - 2024. All rights reserved.