我正在尝试将内容插入学期的description
保存时字段。
// insert stuff into description field of taxonomy
function insert_taxonomy_content( $term_id, $tt_id, $taxonomy ){
// only insert content on certain taxonomies
if ( $taxonomy === \'some_custom_taxonomy\' ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'edit_term\', \'insert_taxonomy_content\');
$content = "Some Content";
$update_args = array(
\'description\' => $content,
);
// update the post, which calls save_post again
wp_update_term( $term_id, $taxonomy, $update_args );
// re-hook this function
add_action(\'edit_term\', \'insert_taxonomy_content\');
}
}
add_action(\'edit_term\', \'insert_taxonomy_content\', 10, 3);
添加内容很有效,但现在我不能再更改现有术语的标题了。我也不能添加新术语。
我认为这为我指明了正确的方向:https://wordpress.stackexchange.com/a/183852/10595
传入的数组与数据库中已有的数据合并。
那么,我如何才能捕获新的标题和/或输入到名称/段塞字段中的段塞,并将其传递给wp_update_term
?
我还根据cjbj\'s suggestion:
// insert stuff into description field of taxonomy
function insert_taxonomy_content( $term_id, $taxonomy ){
// only insert content on certain taxonomies
if ( $taxonomy === \'some_custom_taxonomy\' ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'edit_terms\', \'insert_taxonomy_content\');
$content = "Some Content";
$update_args = array(
\'description\' => $content,
);
// update the post, which calls save_post again
wp_update_term( $term_id, $taxonomy, $update_args );
// re-hook this function
add_action(\'edit_terms\', \'insert_taxonomy_content\');
}
}
add_action(\'edit_terms\', \'insert_taxonomy_content\', 10, 2);
这让我可以再次编辑标题和片段,但现在
description
不会更新。
最合适的回答,由SO网友:Florian 整理而成
所以,多亏了cjbj,我终于找到了正确的答案!我需要使用edited_term
而不是edit_term
. 非常细微的差别。edited_term
保存术语后激发。
// insert stuff into description field of taxonomy
function insert_taxonomy_content( $term_id, $tt_id, $taxonomy ){
// only insert content on certain taxonomies
if ( $taxonomy === \'some_custom_taxonomy\' ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'edited_term\', \'insert_taxonomy_content\');
$content = "Some Content";
$update_args = array(
\'description\' => $content,
);
// update the post, which calls save_post again
wp_update_term( $term_id, $taxonomy, $update_args );
// re-hook this function
add_action(\'edited_term\', \'insert_taxonomy_content\');
}
}
add_action(\'edited_term\', \'insert_taxonomy_content\', 10, 3);