Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 01686270225/85f78e002e058dc6fdfa2e1a4365757a to your computer and use it in GitHub Desktop.
Save 01686270225/85f78e002e058dc6fdfa2e1a4365757a to your computer and use it in GitHub Desktop.
// Remove Parent Category from Child Category URL
add_filter('term_link', 'cdb_no_category_parents', 1000, 3);
function cdb_no_category_parents($url, $term, $taxonomy) {
if($taxonomy == 'category'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
}
return $url;
}
// Rewrite url mới
function cdb_no_category_parents_rewrite_rules($flash = false) {
$terms = get_terms( array(
'taxonomy' => 'category',
'post_type' => 'post',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
add_rewrite_rule($term_slug.'/?$', 'index.php?category_name='.$term_slug,'top');
add_rewrite_rule($term_slug.'/page/([0-9]{1,})/?$', 'index.php?category_name='.$term_slug.'&paged=$matches[1]','top');
add_rewrite_rule($term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name='.$term_slug.'&feed=$matches[1]','top');
}
}
if ($flash == true)
flush_rewrite_rules(false);
}
add_action('init', 'cdb_no_category_parents_rewrite_rules');
/*Sửa lỗi khi tạo mới category bị 404*/
function cdb_new_category_edit_success() {
cdb_no_category_parents_rewrite_rules(true);
}
add_action('created_category','cdb_new_category_edit_success');
add_action('edited_category','cdb_new_category_edit_success');
add_action('delete_category','cdb_new_category_edit_success');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment