Ask your WordPress questions! Pay money and get answers fast! (more info)

Add category slug to woocommerce single product url WordPress

  • REFUNDED

I'm trying to edit the single product page url so that it shows the full hierarchy i.e. includes the parent category and all sub categories in the url. I've managed to do this using this combination: (you can view here http://pbs.transparentwebdesigns.co.uk/)

To remove /product-category/ I use this plugin

http://codecanyon.net/item/woocommerce-seo/3881769?sso?WT.ac=category_item&WT.seg_1=category_item&WT.z_author=Code-Ninjas

Then in Wordpress -> Settings -> Permalinks -> Product permalink base

set to: p/%product_cat%

<strong>Is there a way here to output the category slug instead of category? The issue is that it outputs the title in the url, which doesn't look great. e.g for this products it outputs this:</strong>

http://pbs.transparentwebdesigns.co.uk/p/Roofing Supplies/330mm-16-metre-eaves-protection-5u/

instead of this

http://pbs.transparentwebdesigns.co.uk/p/roofing-supplies/330mm-16-metre-eaves-protection-5u/

So instead of the actual category title Roofing Supplies, it should be the slug roofing-supplies.

A way I've been getting around this is by installing Yoast http://yoast.com/wordpress/ then activating yoast breadcrumbs and the using this to change the titles:

wordpress -> products -> categories ->
then change the name of the category to the same as the slug, e.g. I've done this for Hand Tools and changed title to hand-tools, then at the bottom of the same category you will now have some yoast options just change the Breadcrumbs Title to what you want I changed to Hand Tools.

Now the products output with the correct url i.e. like here

http://pbs.transparentwebdesigns.co.uk/p/hand-tools/hand-saws/bahco-244-fine-cut-saw-22-11-tpi/

BUT
The title on the product page is incorrect, it's showing as hand-tools, instead of Hand Tools

http://pbs.transparentwebdesigns.co.uk/hand-tools/


<strong>I think the easiest way to resolve is to use the category-slug in the Wordpress permalinks settings, so that I don't have to change the category title and use yoast to change the breadcrumb title - question is how do I output the category slug in wordpress permalinks settings?
</strong>

Answers (3)

2013-07-09

Sabby Sam answers:

I guess you need to change your permalink setting in admin panel. In wordpress yoast setting there is an option hide category slug check that one. If you want to change the product slug then try to edit in plugin.


willcm comments:

I wish it was that simple!

What I don't understand is why there is not a category slug i can insert on Wordpress permalink settings,

here http://codex.wordpress.org/Using_Permalinks suggests that %category% will work - but it doesn't?

Is there a way I can manually code a new structure tag that uses the category slug?

2013-07-09

Giri answers:

Hey I think your codecanyon plugin does not sanitize category title. Can you message me your codecanyon plugin? I'll try to fix it..


willcm comments:

I've disabled the codecanyon plugin and still have the same issue, will email you the plugin though

2013-07-09

MDan answers:

Hello,

See this post:
http://davidwalsh.name/wordpress-category-slug
I think you could do this within your .htaccess file.

See also this 3 threads:
http://uploadwp.com/woocommerce-product-category-setup/
http://wordpress.stackexchange.com/questions/43909/woocommerce-permalinks
http://wordpress.stackexchange.com/questions/33551/how-to-rewrite-uri-of-custom-post-type/33555#33555

Hope this helps.


willcm comments:

Sorted myself using this code here:

thanks to this chap - http://blog.peterkling.info/post/31007977292/woocommerce-subcategories-in-permalink

remove_filter( 'post_type_link', 'woocommerce_product_cat_filter_post_link', 10, 2 ); // for woocommerce < 2
remove_filter( 'post_type_link', 'woocommerce_product_post_type_link', 10, 2 ); // for woocommerce >= 2
add_filter( 'post_type_link', 'woocommerce_subcategory_permalink', 10, 2 );
function woocommerce_subcategory_permalink( $permalink, $post ) {

// Abort if post is not a product
if ( $post->post_type !== 'product' )
return $permalink;

// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%product_cat%' ) )
return $permalink;

// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'product_cat' );

if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$permalink = str_replace( '%product_cat%', _x('product', 'slug', 'woocommerce'), $permalink );
} else {
// Get the first term
$first_term = array_shift( $terms );

// Get the hierachical product_category
$parents = woo_get_term_parents( $first_term->term_id, 'product_cat' );

// Replace the placeholder rewrite tag hierachical product_category
$permalink = str_replace( '%product_cat%/', $parents, $permalink );
}

return $permalink;
}


/* Credits: http://wordpress.stackexchange.com/questions/61491/breadcrumb-fix-only-show-parent-category-and-not-childs */
if ( ! function_exists( 'woo_get_term_parents' ) ) {
function woo_get_term_parents( $id, $taxonomy ) {
$chain = '';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;

$name = $parent->slug;

if ( $parent->parent && ( $parent->parent != $parent->term_id ) ) {
$chain .= woo_get_term_parents( $parent->parent, $taxonomy);
}

$chain .= $name."/";
return $chain;
} // End woo_get_term_parents()
}