public function add_rewrite_rule($taxonomy_name, $tax_base, $wp_query_var, $query_var, $rules) { global $wp_rewrite; $new_rules = array(); $taxonomy = get_taxonomy($taxonomy_name); $permalink_structure = get_option('permalink_structure'); $blog_prefix = ''; if (is_multisite() && !is_subdomain_install() && is_main_site() && 0 === strpos($permalink_structure, '/blog/')) { $blog_prefix = 'blog/'; } $terms = hocwp_get_terms($taxonomy_name, array('hide_empty' => false)); if (hocwp_array_has_value($terms)) { foreach ($terms as $term) { $term_nicename = $term->slug; if ($term->parent == $term->cat_ID) { $term->parent = 0; } elseif ($taxonomy->rewrite['hierarchical'] != 0 && $term->parent != 0) { $parents = hocwp_get_term_parents($taxonomy_name, $term->parent, false, '/', true); if (!is_wp_error($parents)) { $term_nicename = $parents . $term_nicename; } unset($parents); } $new_rules[$blog_prefix . '(' . $term_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?' . $wp_query_var . '=$matches[1]&feed=$matches[2]'; $new_rules[$blog_prefix . '(' . $term_nicename . ')/' . $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'] = 'index.php?' . $wp_query_var . '=$matches[1]&paged=$matches[2]'; $new_rules[$blog_prefix . '(' . $term_nicename . ')/?$'] = 'index.php?' . $wp_query_var . '=$matches[1]'; } unset($terms, $term, $term_nicename); } $old_base = $wp_rewrite->get_extra_permastruct($taxonomy_name); $old_base = str_replace('%' . $tax_base . '%', '(.+)', $old_base); $old_base = trim($old_base, '/'); $new_rules[$old_base . '$'] = 'index.php?' . $query_var . '=$matches[1]'; if (hocwp_array_has_value($new_rules)) { $rules = $new_rules; } unset($new_rules, $old_base); return $rules; }
function hocwp_get_term_parents($taxonomy, $id, $link = false, $separator = '/', $nicename = false, $visited = array()) { $chain = ''; $parent = get_term($id, $taxonomy); if (is_wp_error($parent)) { return $parent; } if ($nicename) { $name = $parent->slug; } else { $name = $parent->name; } if ($parent->parent && $parent->parent != $parent->term_id && !in_array($parent->parent, $visited)) { $visited[] = $parent->parent; $chain .= hocwp_get_term_parents($taxonomy, $parent->parent, $link, $separator, $nicename, $visited); } if ($link) { $chain .= '<a href="' . esc_url(get_term_link($parent)) . '">' . $name . '</a>' . $separator; } else { $chain .= $name . $separator; } return $chain; }