Last active
August 22, 2019 18:49
-
-
Save marcelotorres/f19e62f3f5d4a97ac524a6a3fb5f38b4 to your computer and use it in GitHub Desktop.
Create Custom URL structure for specific Post category
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
<?php | |
/* ===== Change 'MY_PRODUCT_CAT_SLUG' to your product cat slug ===== */ | |
function change_product_cat_links( $link, $post) { | |
$terms = wp_get_post_terms( $post->ID, 'product_cat', $args ); | |
if ( $post->post_type == 'product' && $terms[0]->slug == 'MY_PRODUCT_CAT_SLUG') { | |
$link = trailingslashit( home_url('/MY_PRODUCT_CAT_SLUG/' . $post->post_name ) ); | |
} | |
return $link; | |
} | |
add_filter( 'post_type_link', 'change_product_cat_links', 10, 2 ); | |
function product_cat_rewrite_rule() { | |
add_rewrite_rule( '^MY_PRODUCT_CAT_SLUG/([^/]+)/?', 'index.php?product_cat=$matches[1]&post_type=product&name=$matches[1]', 'top' ); | |
} | |
add_action( 'init', 'product_cat_rewrite_rule', 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment