Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active November 19, 2022 05:50
Show Gist options
  • Select an option

  • Save levantoan/b3baea54158280e42260eb124f6e6a5f to your computer and use it in GitHub Desktop.

Select an option

Save levantoan/b3baea54158280e42260eb124f6e6a5f to your computer and use it in GitHub Desktop.

Revisions

  1. levantoan revised this gist Jun 9, 2017. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions [OLD]-remoce-product-category-parent.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <? php
    // Remove product cat base
    add_filter('term_link', 'devvn_no_term_parents', 1000, 3);
    function devvn_no_term_parents($url, $term, $taxonomy) {
    if($taxonomy == 'product_cat'){
    $term_nicename = $term->slug;
    $url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
    }
    return $url;
    }

    // Add our custom product cat rewrite rules
    add_filter('rewrite_rules_array', 'devvn_no_product_cat_parents_rewrite_rules');
    function devvn_no_product_cat_parents_rewrite_rules($rules) {
    $new_rules = array();
    $terms = get_terms( array(
    'taxonomy' => 'product_cat',
    'post_type' => 'product',
    'hide_empty' => false,
    ));
    if($terms && !is_wp_error($terms)){
    foreach ($terms as $term){
    $term_slug = $term->slug;
    $new_rules[$term_slug.'/?$'] = 'index.php?product_cat='.$term_slug;
    $new_rules[$term_slug.'/page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]';
    $new_rules[$term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]';
    }
    }
    return $new_rules + $rules;
    }
  2. levantoan created this gist Apr 4, 2017.
    17 changes: 17 additions & 0 deletions remove-product-category-base-and-parents-slug.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?php
    /***************************
    * Loại bỏ toàn bộ slug của category cha và product-category ra khỏi URL
    * Remove all parents slug product category link
    * Author: http://levantoan.com
    * Example
    Product category url: http://domain.com/product-category/computer
    Product child category url level 1: http://domain.com/product-category/computer/laptop
    Product child category url level 2: http://domain.com/product-category/computer/laptop/dell
    * Change to
    Product category url: http://domain.com/computer
    Product child category url level 1: http://domain.com/laptop
    Product child category url level 2: http://domain.com/dell
    * Code
    Read more here: http://levantoan.com/xoa-bo-product-category-va-toan-bo-slug-cua-danh-muc-cha-khoi-duong-dan-cua-woocommerce/
    ***************************/