Cách để xóa đường dẫn danh mục hay chuyên mục có chữ /category/ trên đường dẫn website sử dụng wordpress, các bạn chỉ cần làm như sau ? Nếu trường hợp code trên không hoạt động thì có thể do WordPress đang cache đang cache cấu trúc đường dẫn, chúng ta cần vào [ Settings ( Cài đặt ) ] => [ Permalinks ( Đường dẫn tĩnh ) ] => bấm [ Save changes ] để cập nhật lại cấu trúc đường dẫn.
Thêm đoạn code sau vào file functions.php
/* actions */ add_action( 'created_category', 'remove_category_url_refresh_rules' ); add_action( 'delete_category', 'remove_category_url_refresh_rules' ); add_action( 'edited_category', 'remove_category_url_refresh_rules' ); add_action( 'init', 'remove_category_url_permastruct' ); /* filters */ add_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); add_filter( 'query_vars', 'remove_category_url_query_vars' ); // Adds 'category_redirect' query variable add_filter( 'request', 'remove_category_url_request' ); // Redirects if 'category_redirect' is set function remove_category_url_refresh_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } function remove_category_url_deactivate() { remove_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); // We don't want to insert our custom rules again remove_category_url_refresh_rules(); } /** * Removes category base. * * @return void */ function remove_category_url_permastruct() { global $wp_rewrite, $wp_version; if ( 3.4 <= $wp_version ) { $wp_rewrite->extra_permastructs['category']['struct'] = '%category%'; } else { $wp_rewrite->extra_permastructs['category'][0] = '%category%'; } } /** * Adds our custom category rewrite rules. * * @param array $category_rewrite Category rewrite rules. * * @return array */ function remove_category_url_rewrite_rules( $category_rewrite ) { global $wp_rewrite; $category_rewrite = array(); /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */ if ( class_exists( 'Sitepress' ) ) { global $sitepress; remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) ); $categories = get_categories( array( 'hide_empty' => false, '_icl_show_all_langs' => true ) ); add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) ); } else { $categories = get_categories( array( 'hide_empty' => false ) ); } foreach ( $categories as $category ) { $category_nicename = $category->slug; if ( $category->parent == $category->cat_ID ) { $category->parent = 0; } elseif ( 0 != $category->parent ) { $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename; } $category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; $category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; $category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]'; } // Redirect support from Old Category Base $old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category'; $old_category_base = trim( $old_category_base, '/' ); $category_rewrite[ $old_category_base . '/(.*)$' ] = 'index.php?category_redirect=$matches[1]'; return $category_rewrite; } function remove_category_url_query_vars( $public_query_vars ) { $public_query_vars[] = 'category_redirect'; return $public_query_vars; } /** * Handles category redirects. * * @param $query_vars Current query vars. * * @return array $query_vars, or void if category_redirect is present. */ function remove_category_url_request( $query_vars ) { if ( isset( $query_vars['category_redirect'] ) ) { $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['category_redirect'], 'category' ); status_header( 301 ); header( "Location: $catlink" ); exit; } return $query_vars; }
Tagged with: code xóa category, share code, xóa url category
Dưới là đoạn code thay đổi chữ Add to cart và thêm giỏ hàng của woo năm 2023, phiên bản mới nhất cho wordpress...
Bạn có biết bạn có thể hiển thị nhiều đồng hồ trong Windows 11/10 không? Bạn có thể để Windows 11/10/8/7 hiển thị...
Bảng tổng hợp thật tuyệt vời! Chúng là một trong những tính năng mạnh mẽ nhất của Excel , chúng cho phép bạn nhanh...
Tìm cửa hàng bán Ghế sofa ở tại Hải Phòng, đơn vị cung cấp và sản xuất ghế sofa uy tín – chuyên nghiệp, nơi...
Thêm mục đăng bài trong danh mục sản phẩm " Add editor to Wordpress WooCommerce Product Category Page ", nhiều người đang cần...
Hướng dẫn việc tích hợp kết nối API lấy dữ liệu từ Contact Form 7 tới Google Sheet là một vấn đề đang được...
Bình luận bài viết
Đăng nhập để bình luận và xem các bình luận khác.