Gọi lại
cho tôi

Hôm nay mình xin hướng dẫn các bạn các bỏ những Slug không cần thiết trên URL trang web.

Có 3 phần chính là:

1.Xóa bỏ slug danh-muc khi vào trang danh mục sản phẩm và Xóa bỏ slug san-pham khi vào trang sản phẩm chi tiết.

2.Xóa bỏ Slug Featured_item và featured_item_category khi vào trang Portfolio và Porfolio category

3.Xóa bỏ Slug category khi vào trang danh mục bài viết

Đối với phần 1 và 2, các bạn chỉ cần copy đoạn code tương ứng bỏ vào file Functions.php của themes đang sử dụng. Đối với các themes mua tại Thiết Kế Web Chuyên , Tên themes mặc định là thiet-ke-web-chuyen.

Sau đây mình xin đi vào chi tiết từng phần:

1.Xóa bỏ slug danh-muc khi vào trang danh mục sản phẩm và Xóa bỏ slug san-pham khi vào trang sản phẩm chi tiết.

Đoạn code để xóa bỏ slug danh-muc khi vào trang danh mục sản phẩm:

/*
* Remove product-category in URL
* Thay danh-muc bằng slug hiện tại của bạn. Themes tại Thiết Kế Web Chuyên - Mặc định là danh-muc
*/
add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 );
function devvn_product_cat_permalink( $url, $term, $taxonomy ){
    switch ($taxonomy):
        case 'product_cat':
            $taxonomy_slug = 'danh-muc'; //Thay bằng slug hiện tại của bạn. Mặc định Của WKN là danh-muc
            if(strpos($url, $taxonomy_slug) === FALSE) break;
            $url = str_replace('/' . $taxonomy_slug, '', $url);
            break;
    endswitch;
    return $url;
}
// Add our custom product cat rewrite rules
function devvn_product_category_rewrite_rules($flash = false) {
    $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'post_type' => 'product',
        'hide_empty' => false,
    ));
    if($terms && !is_wp_error($terms)){
        $siteurl = esc_url(home_url('/'));
        foreach ($terms as $term){
            $term_slug = $term->slug;
            $baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
            add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top');
            add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top');
            add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top');
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_product_category_rewrite_rules');
/*Sửa lỗi khi tạo mới taxomony bị 404*/
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 );
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) {
    devvn_product_category_rewrite_rules(true);
}

Đoạn code xỏa bỏ slug san-pham khi vào trang sản phẩm chi tiết.

/*
* Code Bỏ /san-pham/ hoặc ... có hỗ trợ dạng %product_cat%
*/
function devvn_remove_slug( $post_link, $post ) {
    if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
        return $post_link;
    }
    if('product' == $post->post_type){
        $post_link = str_replace( '/san-pham/', '/', $post_link ); //Thay cua-hang bằng slug hiện tại của bạn
    }else{
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 );
/*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/
function devvn_woo_product_rewrite_rules($flash = false) {
    global $wp_post_types, $wpdb;
    $siteLink = esc_url(home_url('/'));
    foreach ($wp_post_types as $type=>$custom_post) {
        if($type == 'product'){
            if ($custom_post->_builtin == false) {
                $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
                            FROM {$wpdb->posts} 
                            WHERE {$wpdb->posts}.post_status = 'publish' 
                            AND {$wpdb->posts}.post_type = '{$type}'";
                $posts = $wpdb->get_results($querystr, OBJECT);
                foreach ($posts as $post) {
                    $current_slug = get_permalink($post->ID);
                    $base_product = str_replace($siteLink,'',$current_slug);
                    add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');
                }
            }
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_woo_product_rewrite_rules');
/*Fix lỗi khi tạo sản phẩm mới bị 404*/
function devvn_woo_new_product_post_save($post_id){
    global $wp_post_types;
    $post_type = get_post_type($post_id);
    foreach ($wp_post_types as $type=>$custom_post) {
        if ($custom_post->_builtin == false && $type == $post_type) {
            devvn_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'devvn_woo_new_product_post_save');

2.Xóa bỏ Slug Featured_item và featured_item_category khi vào trang Portfolio và Porfolio category

Đoạn code bỏ Feature_item trong Slug Porfolio:

function ah_remove_custom_post_type_slug( $post_link, $post, $leavename ) {
 
    if ( ! in_array( $post->post_type, array( 'featured_item' ) ) || 'publish' != $post->post_status )
        return $post_link;
 
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
 
    return $post_link;
}
add_filter( 'post_type_link', 'ah_remove_custom_post_type_slug', 10, 3 );
 
function ah_parse_request_tricksy( $query ) {
    
    if ( ! $query->is_main_query() )
        return;
 
    if ( 2 != count( $query->query )
        || ! isset( $query->query['page'] ) )
        return;
 
    if ( ! empty( $query->query['name'] ) )
        $query->set( 'post_type', array( 'post', 'featured_item', 'page' ) );
}
add_action( 'pre_get_posts', 'ah_parse_request_tricksy' );

Đoạn code bỏ Feature_item_category trong slug danh mục Portfolio:

add_filter('request', 'rudr_change_term_request', 1, 1 );
 
function rudr_change_term_request($query){
 
    $tax_name = 'featured_item_category'; // specify you taxonomy name here, it can be also 'category' or 'post_tag'
 
    // Request for child terms differs, we should make an additional check
    if( $query['attachment'] ) :
        $include_children = true;
        $name = $query['attachment'];
    else:
        $include_children = false;
        $name = $query['name'];
    endif;
 
 
    $term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
 
    if (isset($name) && $term && !is_wp_error($term)): // check it here
 
        if( $include_children ) {
            unset($query['attachment']);
            $parent = $term->parent;
            while( $parent ) {
                $parent_term = get_term( $parent, $tax_name);
                $name = $parent_term->slug . '/' . $name;
                $parent = $parent_term->parent;
            }
        } else {
            unset($query['name']);
        }
 
        switch( $tax_name ):
            case 'category':{
                $query['category_name'] = $name; // for categories
                break;
            }
            case 'post_tag':{
                $query['tag'] = $name; // for post tags
                break;
            }
            default:{
                $query[$tax_name] = $name; // for another taxonomies
                break;
            }
        endswitch;
 
    endif;
 
    return $query;
 
}
 
 
add_filter( 'term_link', 'rudr_term_permalink', 10, 3 );
 
function rudr_term_permalink( $url, $term, $taxonomy ){
 
    $taxonomy_name = 'featured_item_category'; // your taxonomy name here
    $taxonomy_slug = 'featured_item_category'; // the taxonomy slug can be different with the taxonomy name (like 'post_tag' and 'tag' )
 
    // exit the function if taxonomy slug is not in URL
    if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
 
    $url = str_replace('/' . $taxonomy_slug, '', $url);
 
    return $url;
}

3.Xóa bỏ Slug category khi vào trang danh mục bài viết:

Đối với các theme mua tại Thiết Kế Web Chuyên – đã có cài sẵn Yoast Seo – Các bạn chỉ cần bật tính năng Advanced của Yoast Seo lên và vào phần Permalink Chọn Remove category:

Làm sao xóa bỏ những Slug không cần thiết trên URL trang web 1

Vào phần tùy chỉnh Advanced -> Sau đó vào phần Permalink:

Làm sao xóa bỏ những Slug không cần thiết trên URL trang web 2

Tại đây ở phẩn Permalinks -> Chọn Remove /category/ để bỏ slug phần này ở mục danh mục bài viết

Làm sao xóa bỏ những Slug không cần thiết trên URL trang web 3

Sau khi hoàn tất các phần này, các bạn sẽ có một hệ thống link đơn giản và thân thiện cho Seo. Chúc các bạn

Đọc thêm

ThietKeWebChuyen.Com ( Since 2013 )