简体   繁体   中英

WordPress > using wp_insert_category() to separate “category_nicename” from category slug or “cat_name”

I'd like to use wp_insert_category() to create a category that's displayed as "My Category" but has a category slug of "test-my-category".

Can I do this with wp_insert_category? Anyone have an example?

Yes you can. Do the following:

$category = array('cat_name'=>'My Category', 'category_nicename'=>'test-my-category');
wp_insert_category($category);

cat_name is used for the title of the category whilst category_nicename is used for the slug.

Function Reference:wp insert category « WordPress Codex

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

See http://codex.wordpress.org/Function_Reference/wp_insert_term

Initial Taxonomies include: 'category', 'post_tag', 'nav_menu', 'link_category' (defined in wp-includes/taxonomy.php)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM