Last active
May 13, 2020 06:54
-
-
Save kmaroff/e93679729664985cfae03a3594aa68f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //1. Для главной страницы магазина устанавливаем любую страницу со слагом shop. | |
| //2. В настройках постоянных ссылок для базы категорий товаров указываем shop, а для товаров выбираем произвольную базу и вписываем туда /shop/%product_cat%/. | |
| //3. Далее, используя сниппет ниже (его можно добавить в functions.php или в mu-plugins), создаём новые правила перенаправления для каждой категории товара, подменяя в ссылках на категорию /product-category/ на /shop/. | |
| //4. В самом конце: зайдите на страницу Настройки -> Постоянные ссылки для сброса правил перенаправлений. | |
| <?php | |
| /** | |
| * Делаем простую и понятную структуру для урлов WooCommerce: | |
| * | |
| * Страница магазина: https://domain.com/shop | |
| * Страница категории: https://domain.com/shop/category | |
| * Страница товара: https://domain.com/shop/category | |
| * | |
| * @link https://rogodessa.ru/woocommerce/woocommerce-nastrojki-postoyannyx-ssylok-kak-ubrat-product-category | |
| */ | |
| /** | |
| * Генерим свои реврайты для категорий товаров | |
| * и сбрасываем их кэш при необходимости. | |
| * | |
| * @param bool $flash флаг, показывающий нужно ли сбрасывать пермалинки. | |
| */ | |
| function mihdan_woocommerce_permalinks( $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 ( true === $flash ) { | |
| flush_rewrite_rules( false ); | |
| } | |
| } | |
| add_filter( 'init', 'mihdan_woocommerce_permalinks' ); | |
| /** | |
| * Перегенирить пермалинки и сбросить их кэш при создании новой категории. | |
| * | |
| * @param int $term_id идентификатор категории. | |
| * @param string $taxonomy слаг таксономии. | |
| */ | |
| function mihdan_woocommerce_permalinks_flush( $term_id, $taxonomy ) { | |
| if ( 'product_cat' === $taxonomy ) { | |
| mihdan_woocommerce_permalinks( true ); | |
| } | |
| } | |
| add_action( 'create_term', 'mihdan_woocommerce_permalinks_flush', 10, 2 ); | |
| /** | |
| * Подменить /product-category/ на /shop/ в ссылке категории. | |
| * | |
| * @param string $url URL по дефолту. | |
| * @param object $term объект термина. | |
| * @param string $taxonomy слаг таксономии. | |
| * | |
| * @return mixed | |
| */ | |
| function mihdan_woocommerce_fixed_category_permalink( $url, $term, $taxonomy ) { | |
| if ( 'product_cat' === $taxonomy ) { | |
| return str_replace( 'product-category/', 'shop/', $url ); | |
| } | |
| return $url; | |
| } | |
| add_filter( 'term_link', 'mihdan_woocommerce_fixed_category_permalink', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment