Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //FoxyShop URL Customization (FoxyShop 4.2+ required)
- //This code will add the product category to the url.
- //IMPORTANT: put this in your wp-config.php file:
- //define('FOXYSHOP_PRODUCTS_SLUG','products/%foxyshop_categories%');
- add_filter( 'post_type_link', 'custom_foxyshop_product_link', 10, 3 );
- function custom_foxyshop_product_link( $permalink, $post, $leavename ) {
- global $wp_query;
- $term_url = '';
- $rewritecode = array(
- '%foxyshop_categories%',
- $leavename ? '' : '%postname%',
- );
- $post_id = $post;
- $post = get_post( $post_id );
- // Only applies to Foxyshop products, don't stop on permalinks of other CPTs
- if ($post->post_type != 'foxyshop_product')
- return $permalink;
- $permalink_structure = get_option( 'permalink_structure' );
- $our_permalink_structure = FOXYSHOP_PRODUCTS_SLUG . "/%postname%/";
- if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
- $post_name = $post->post_name;
- $product_categories = wp_get_post_terms($post->ID, 'foxyshop_categories');
- $category_name = "";
- foreach($product_categories as $term) {
- $category_name = $term->slug;
- if ($term->parent > 0) {
- $parent_cats = get_ancestors($term->term_id, 'foxyshop_categories');
- foreach($parent_cats as $parent_id) {
- $parent_term = get_term_by('id',$parent_id,'foxyshop_categories');
- $category_name = $parent_term->slug . "/" . $category_name;
- }
- }
- continue;
- }
- if ($category_name == '') $category_name = "uncategorized";
- $rewritereplace = array(
- $category_name,
- $post_name
- );
- $permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
- $permalink = user_trailingslashit( $permalink, 'single' );
- $permalink = home_url( $permalink );
- }
- return apply_filters( 'foxyshop_product_permalink', $permalink, $post->ID );
- }
- add_filter('foxyshop_product_url_slug', 'my_foxyshop_product_url_slug', 10, 2);
- function my_foxyshop_product_url_slug($slug, $id) {
- if (strpos($slug, '%foxyshop_categories%') !== false) {
- $product_categories = wp_get_post_terms($id, 'foxyshop_categories');
- $category_name = "";
- foreach($product_categories as $term) {
- $category_name = $term->slug;
- if ($term->parent > 0) {
- $parent_cats = get_ancestors($term->term_id, 'foxyshop_categories');
- foreach($parent_cats as $parent_id) {
- $parent_term = get_term_by('id',$parent_id,'foxyshop_categories');
- $category_name = $parent_term->slug . "/" . $category_name;
- }
- }
- continue;
- }
- if (!$category_name) $category_name = "base";
- $slug = str_replace('%foxyshop_categories%', $category_name, $slug);
- }
- return $slug;
- }
- add_filter('foxyshop_template_redirect_product_slug', 'my_foxyshop_template_redirect_product_slug');
- function my_foxyshop_template_redirect_product_slug($str) {
- return str_replace('/%foxyshop_categories%', '', $str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement