我有几个类别需要更新/分配父类别,以便默认类别(未分类)成为父类别。
通过类别管理器很容易做到这一点,但是,我需要通过脚本来做到这一点。
答案 0 :(得分:1)
wp_update_cagetory()的原始source code是:
/**
* Aliases wp_insert_category() with minimal args.
*
* If you want to update only some fields of an existing category, call this
* function with only the new values set inside $catarr.
*
* @since 2.0.0
*
* @param array $catarr The 'cat_ID' value is required. All other keys are optional.
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
*/
function wp_update_category($catarr) {
$cat_ID = (int) $catarr['cat_ID'];
if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
return false;
// First, get all of the original fields
$category = get_category($cat_ID, ARRAY_A);
// Escape data pulled from DB.
$category = add_magic_quotes($category);
// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge($category, $catarr);
return wp_insert_category($catarr);
}
所以这似乎是不言自明的:
$modifiedCategory = array();
$modifiedCategory['cat_ID'] = 12; // This should be your modified category ID
$modifiedCategory['category_parent'] = 1; // This should be your desired parent category (in my case 1 == Uncategorized)
wp_update_category($modifiedCategory);
对您需要更新的所有类别重复上述步骤。
<强>更新强>
与类别ID检索问题有关 - 似乎 category_exists()函数应该完成这项工作:
function category_exists($cat_name, $parent = 0) {
$id = is_term($cat_name, 'category', $parent);
if ( is_array($id) )
$id = $id['term_id'];
return $id;
}
所以这应该有效,但我没有测试它:
$modifiedCategory['cat_ID'] = category_exists('some category');
$modifiedCategory['category_parent'] = category_exists('Uncategorized');