plugin fixes
This commit is contained in:
@@ -682,18 +682,49 @@ function igny8_process_categories($categories, $post_id) {
|
||||
}
|
||||
// If it's a string (name or slug)
|
||||
elseif (is_string($category)) {
|
||||
$term = get_term_by('slug', $category, 'category');
|
||||
if (!$term) {
|
||||
$term = get_term_by('name', $category, 'category');
|
||||
}
|
||||
|
||||
if ($term && !is_wp_error($term)) {
|
||||
$term_id = $term->term_id;
|
||||
// Check if it's a hierarchical category (e.g., "Gardening > Plant Care")
|
||||
if (strpos($category, ' > ') !== false) {
|
||||
$parts = array_map('trim', explode(' > ', $category));
|
||||
$parent_id = 0;
|
||||
|
||||
// Process each level of the hierarchy
|
||||
foreach ($parts as $part) {
|
||||
$term = get_term_by('name', $part, 'category');
|
||||
|
||||
if (!$term || is_wp_error($term)) {
|
||||
// Create term with parent
|
||||
$term_result = wp_insert_term($part, 'category', array(
|
||||
'parent' => $parent_id,
|
||||
'slug' => sanitize_title($part)
|
||||
));
|
||||
if (!is_wp_error($term_result)) {
|
||||
$parent_id = $term_result['term_id'];
|
||||
$term_id = $term_result['term_id']; // Last one is what we assign
|
||||
}
|
||||
} else {
|
||||
// Update parent if it doesn't match
|
||||
if ($parent_id > 0 && $term->parent != $parent_id) {
|
||||
wp_update_term($term->term_id, 'category', array('parent' => $parent_id));
|
||||
}
|
||||
$parent_id = $term->term_id;
|
||||
$term_id = $term->term_id; // Last one is what we assign
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Create new category
|
||||
$term_result = wp_insert_term($category, 'category');
|
||||
if (!is_wp_error($term_result)) {
|
||||
$term_id = $term_result['term_id'];
|
||||
// Simple category (no hierarchy)
|
||||
$term = get_term_by('slug', $category, 'category');
|
||||
if (!$term) {
|
||||
$term = get_term_by('name', $category, 'category');
|
||||
}
|
||||
|
||||
if ($term && !is_wp_error($term)) {
|
||||
$term_id = $term->term_id;
|
||||
} else {
|
||||
// Create new category
|
||||
$term_result = wp_insert_term($category, 'category');
|
||||
if (!is_wp_error($term_result)) {
|
||||
$term_id = $term_result['term_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user