Code xóa bỏ /category/ và slug category cha không dùng plugin

Đánh giá post

Việc bạn cần làm là copy đoạn code bên dưới vào file functions.php trong theme đang sử dụng sau đó vào update lại permalink tại (Settings -> Permalink -> Save change)

// Remove Parent Category from Child Category URL

add_filter('term_link', 'devvn_no_category_parents', 1000, 3);

function devvn_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 devvn_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', 'devvn_no_category_parents_rewrite_rules');




/*Sửa lỗi khi tạo mới category bị 404*/

function devvn_new_category_edit_success() {

devvn_no_category_parents_rewrite_rules(true);

}

add_action('created_category','devvn_new_category_edit_success');

add_action('edited_category','devvn_new_category_edit_success');

add_action('delete_category','devvn_new_category_edit_success');
Đánh giá post


  1. [url=https://happyfamilymedicalstore.online/]no prescription required pharmacy[/url]

  2. I was wondering if you ever considered changing the page layout of your website?
    Its very well written; I love what youve got
    to say. But maybe you could a little more in the way of
    content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or two
    images. Maybe you could space it out better?

  3. Hi everyone, it’s my first pay a quick visit at this website, and piece of writing is genuinely
    fruitful in favor of me, keep up posting these
    content.

    Check out my web page; dune couch

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *