Header Ads Widget

Xóa bỏ product-category trong url woocommerce

Xóa bỏ product-category trong url woocommerce

Khi cài woocommerce đường dẫn url danh mục sản phẩm của bạn sẽ có dạng như:chiaseaz.com/product-category/thoi-trang, bạn muốn url có dạng:như:chiaseaz.com/thoi-trang sẽ dùng Plugin

Cách làm như sau:
Vào quản trị trong web bán hàng của bạn sau đó chọn Plugins -> Add New và search tìm plugin WP htaccess Control và cài đặt.
Sau khi cài đặt xong bạn truy cập vào Settings -> htaccess Control để vào thiết lập trong plugin WP htaccess Control.
Tại bảng thiết lập của plugin bạn click vào Remove Taxonomies and Author Base để mở rộng thiết lập ra. Bạn sẽ thấy các tùy chọn như hình dưới. Ở đây bạn tick vào mục Remove Product Categories Base để xóa bỏ product-category trong link woocommerce. Sau đó kéo xuống và ấn Save all changes để lưu cài đặt

Xoá bỏ san-pham khi click vào chi tiết một sản phẩm thì thêm đoạn code sau vào trong file functions.php

$args = array(    'description' => 'Chi tiết sản phẩm',    'label' => __('Sản phẩm'),    'public' => true,    'rewrite' => array( 'slug' => '/'),);register_post_type( 'product' , $args );/** * Remove the slug from published post permalinks. */function custom_remove_cpt_slug( $post_link, $post, $leavename ) {     if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {        return $post_link;    }     $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );     return $post_link;}add_filter( 'post_type_link', 'custom_remove_cpt_slug', 10, 3 );/** * Some hackery to have WordPress match postname to any of our public post types * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts * Typically core only accounts for posts and pages where the slug is /post-name/ */function custom_parse_request_tricksy( $query ) {     // Only noop the main query    if ( ! $query->is_main_query() )        return;     // Only noop our very specific rewrite rule match    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {        return;    }     // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match    if ( ! empty( $query->query['name'] ) ) {        $query->set( 'post_type', array( 'post', 'product', 'page' ) );    }}add_action( 'pre_get_posts', 'custom_parse_request_tricksy' );

Nhận xét