如何将默认项添加到自定义分类中?

时间:2011-01-18 作者:janoChen

Wordpress默认分类法(Categories)默认情况下未对项目进行分类。如何将默认项添加到新的自定义分类法?

functions.php:

// === CUSTOM TAXONOMIES === //
function my_custom_taxonomies() {
    register_taxonomy(
        \'block\',        // internal name = machine-readable taxonomy name
        \'static_content\',       // object type = post, page, link, or custom post-type
        array(
            \'hierarchical\' => true,
            \'labels\' => array(
                \'name\' => __( \'Blocks\' ),
                \'singular_name\' => __( \'Block\' ),
                \'add_new_item\' => \'Add New Block\',
                \'edit_item\' => \'Edit Block\',
                \'new_item\' => \'New Block\',
                \'search_items\' => \'Search Block\',
                \'not_found\' => \'No Block found\',
                \'not_found_in_trash\' => \'No Block found in trash\',
            ),
            \'query_var\' => true,    // enable taxonomy-specific querying
            \'rewrite\' => array( \'slug\' => \'block\' ),    // pretty permalinks for your taxonomy?
        )
    );
}
add_action(\'init\', \'my_custom_taxonomies\', 0);
EDIT: I just want to have the taxonomy item there when the theme is installed. It doesn\'t have to automatically be added to any empty term.

5 个回复
最合适的回答,由SO网友:anu 整理而成

请看这里:

https://web.archive.org/web/20150403012347/http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/

基本上,您需要做的是使用save\\u post挂钩来检查帖子的术语,并在分类法中添加默认术语(如果为空)。

如果您只想在自定义分类法中设置初始术语,那么可以使用wp_insert_term(). 在创建自定义分类法的同一个函数中添加它可能是最简单的。当t3ios在评论中添加时,您应该调用get_term() 首先,仅当返回值为null时插入术语(即术语不存在)。

此示例代码来自Codex:http://codex.wordpress.org/Function_Reference/wp_insert_term

$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
  )
);

SO网友:Rarst

默认类别为硬编码大小写wp_insert_post() 作用

所以它不能被精确地复制,但你可以用其他方式来处理它。我会尝试post status transition 对于新帖子,如果在帖子创建过程中未分配任何内容,则指定所需的默认术语。

SO网友:amarinediary

自2020年起更新版本描述5.5.0default_term 参数version 5.5.0 发布时,您现在可以使用非常方便的default_term argument.

$args = [
    /...
    \'default_term\' => [ //(string|array) Default term to be used for the taxonomy.
        \'name\' => \'Potato\', //(string) Name of default term.
        \'slug\' => \'potato\', //(string) Slug for default term.
        \'description\' => \'You can spend the night inside a potato, https://www.youtube.com/watch?v=h2mj-7-Zklw\', //(string) Description for default term.
    ],
    /...
];

SO网友:endle.winters

我需要用一周中的天数填充自定义分类法“Days”。。我不想让客户在创建天的过程中遇到麻烦,也不想在那里删除天或拼写错误的天。根据上述建议,我想到了这个,但我想知道是否有更简洁的编码方式:

 /*************************************** ...Create a Custom Taxonomy for days ******************************/
add_action( \'init\', \'build_taxonomies\', 0 );  
function build_taxonomies() {  
    register_taxonomy( 
    \'days\', 
    \'schedule\',
   array( \'hierarchical\' => true, 
    \'label\' => \'Days\',
    \'query_var\' => true, 
    \'show_ui\' => false, //removes the menus from admin menu and edit panel  
    \'rewrite\' => true ) );  

/*---------------------------------------Check to see if the days are created..if not, create them----*/
$parent_term = term_exists( \'days\', \'days\' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term[\'term_id\']; // get numeric term id

wp_insert_term(//this should probably be an array, but I kept getting errors..
        \'Monday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'monday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Tuesday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'tuesday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Wednesday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'wednesday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Thursday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'thursday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Friday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'friday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Saturday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'saturday\',
        \'parent\'=> $parent_term_id ));

wp_insert_term(
        \'Sunday\', // the term 
        \'days\', // the taxonomy
        array(
        \'slug\' => \'sunday\',
        \'parent\'=> $parent_term_id ));
}
/************ now I add my own meta box for days to get rid of extra controls *************/

add_action(\'admin_menu\', \'add_custom_categories_box\');
function add_custom_categories_box() {
 add_meta_box(\'myrelateddiv\', \'Days*\', \'ilc_post_related_meta_box\', \'schedule\', \'normal\', \'low\', array( \'taxonomy\' => \'days\' ));
}

function ilc_post_related_meta_box( $post, $box ) {
  $defaults = array(\'taxonomy\' => \'related\');
  if ( !isset($box[\'args\']) || !is_array($box[\'args\']) )
  $args = array();
  else
  $args = $box[\'args\'];
  extract( wp_parse_args($args, $defaults), EXTR_SKIP );
  $tax = get_taxonomy($taxonomy);
?>

  <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
<?php
  wp_terms_checklist($post->ID, array( \'taxonomy\' => $taxonomy, \'popular_cats\' => $popular_ids, \'checked_ontop\' => FALSE ) )
?>
</ul>   

SO网友:Allan Christian Carlos

使用Default Term 插件你可以这样做

register_taxonomy( \'custom-tax\', array(\'post\'), array(
    \'label\'              => \'Custom Tag\',
    \'public\'             => true,
    \'show_ui\'            => true,
    \'default_term\'       => \'Some Default Term\', // Add this line to your code 
// then activate and deactivate the default term plugin to save the terms you set.
));
默认情况下,提交帖子时,如果没有选中术语,则会将默认术语保存到帖子中。它适用于层次和非层次分类法。

结束

相关推荐

Saving Taxonomy Terms

我有一个有趣的问题,希望有人能尽快回答。我已经创建了自己的metabox,它基于“我的metabox代码”(下面的列表)正确地显示了我创建的“event\\u types”分类中所有术语的下拉列表。我遇到的问题是,当从下拉列表中选择不同的术语并更新帖子时,能够保存/更新与帖子相关的术语。在对各种代码位进行修补之后,我发现通过手动将term\\u ID number[用逗号分隔]输入数组区域,我得到了我想要的结果。例如,如果在保存帖子时,函数将调用此代码wp_set_post_terms( $post_id