Hướng dẫn loại bỏ chữ product-category trong đường dẫn sản phẩm woocommerce
Bạn chỉ cần copy đoạn code sau vào file functions.php của theme đang sử dụng sau đó vào update lại permalink (Setting -> Permalink -> Save change) là có thể xóa bỏ dc chữ product-category ra khỏi đường dẫn rồi.
Chú ý:
- Cú pháp PHP khi copy
- Tại dòng 10 code dưới áp dụng cho slug danh mục là “product-category”. Hãy thay slug đó thành slug hiện tại của web bạn (Vào Setting -> Permalink để ktra)
<?php
/*
* Remove product-category in URL
* Thay product-category bằng slug hiện tại của bạn. Mặc định là product-category
*/
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 = 'product-category'; //Thay bằng slug hiện tại của bạn. Mặc định là product-category
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);
}
Chúc các bạn thành công!
Bài viết liên quan
- 07/02/2023
21.Code comment bằng facebook cho wordpress <div class=”cmt”> <div class=”fb-comments” data-width=”100%” data-href=”<?php the_permalink(); ?>” data-numposts=”3″></div> </div> <div id=”fb-root”></div> <script> (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = “//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7&appId=750688268378229”; fjs.parentNode.insertBefore(js, fjs); }(document, ‘script’, ‘facebook-jssdk’)); </script>
Đọc thêm
- 06/02/2023
20.Code lấy 10 comment mới nhất <?php $cmt = get_comments(array( ‘status’ => ‘approve’, ‘number’=> 10, )); ?> <div class=”content-w content-news”> <ul> <?php foreach ($cmt as $value) { ?> <li> <a href=”<?php the_permalink($value->comment_post_ID);?>#comment-<?php echo $value->comment_ID; ?>”><?php echo get_avatar($value->comment_author_email, 150 ); ?></a> <a href=”<?php the_permalink($value->comment_post_ID); ?>#comment-<?php echo $value->comment_ID; ?>”><?php echo $value->comment_author; ?></a> – <span style=”color: #cd8a35;font-size: 12px;”><?php […]
Đọc thêm
- 05/02/2023
19.Code lấy bài viết ngẫu nhiên <?php $postquery = new WP_Query(array(‘posts_per_page’ => 35, ‘orderby’ => ‘rand’)); if ($postquery->have_posts()) { while ($postquery->have_posts()) : $postquery->the_post(); $do_not_duplicate = $post->ID; ?> <li> <a href=”<?php the_permalink();?>”><?php the_title();?></a> </li> <?php endwhile; } wp_reset_postdata(); ?> Đoạn code trên lấy 35 bài viết ngẫu nhiên.
Đọc thêm
- 04/02/2023
18.Code query bài viết theo custom filed Query 1 custom filed <?php // args $args = array( ‘numberposts’ => -1, ‘post_type’ => ‘event’, ‘meta_key’ => ‘location’, ‘meta_value’ => ‘Melbourne’ ); // query $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href=”<?php […]
Đọc thêm