1
我有一個自定義帖子類型稱爲「產品」與自定義分類「作者」。現在我有另一種稱爲「作者」的自定義帖子類型。WordPress的:當我添加一篇文章,這個詞將自動創建?
我想這樣:當我在我的文章類型「作者」中發佈新帖子時,將在我的分類「作者」中自動創建一個具有相同標題的新詞語。可能嗎?
感謝您的幫助。
我有一個自定義帖子類型稱爲「產品」與自定義分類「作者」。現在我有另一種稱爲「作者」的自定義帖子類型。WordPress的:當我添加一篇文章,這個詞將自動創建?
我想這樣:當我在我的文章類型「作者」中發佈新帖子時,將在我的分類「作者」中自動創建一個具有相同標題的新詞語。可能嗎?
感謝您的幫助。
請試試這可能會對你有幫助。
function update_custom_terms($post_id) {
// only update terms if it's a authors post
if ('authors' != get_post_type($post_id)) {
return;
}
// don't create or update terms for system generated posts
if (get_post_status($post_id) == 'auto-draft') {
return;
}
/*
* Grab the post title and slug to use as the new
* or updated term name and slug
*/
$term_title = get_the_title($post_id);
$term_slug = get_post($post_id)->post_name;
/*
* Check if a corresponding term already exists by comparing
* the post ID to all existing term descriptions.
*/
$existing_terms = get_terms('author', array(
'hide_empty' => false
)
);
foreach($existing_terms as $term) {
if ($term->description == $post_id) {
//term already exists, so update it and we're done
wp_update_term($term->term_id, 'author', array(
'name' => $term_title,
'slug' => $term_slug
)
);
return;
}
}
/*
* If we didn't find a match above, this is a new post,
* so create a new term.
*/
wp_insert_term($term_title, 'author', array(
'slug' => $term_slug,
'description' => $post_id
)
);
}
//run the update function whenever a post is created or edited
add_action('save_post', 'update_custom_terms');
其工作出色!多謝了朋友! – admiva