[英]WooCommerce custom slug for specific product category
I try to create a custom permalink for products in a specific product category but it works really weird.我尝试为特定产品类别中的产品创建自定义永久链接,但它的工作方式非常奇怪。
I try to achieve URLs like:我尝试实现如下 URL:
I tried to add the %type% rule to product permalinks by using a two-step approach to (1) generate proper URLs and (2) show respective product page:我尝试通过使用两步方法(1)生成正确的 URL 和(2)显示相应的产品页面,将 %type% 规则添加到产品永久链接:
function custom_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%type%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'product_cat');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
if ($terms[0]->slug == 'category_name') {
$taxonomy_slug = $terms[0]->slug;
} else {
$taxonomy_slug = 'default_slug';
}
}
else {
$taxonomy_slug = 'default_slug';
}
return str_replace('%type%', $taxonomy_slug, $permalink);
}
add_filter('post_link', 'custom_permalink', 10, 3);
add_filter('post_type_link', 'custom_permalink', 10, 3);
function rewrite_product_base_tag() {
add_rewrite_tag( '%type%', '([^&]+)', 'in-type=' );
}
add_action( 'init', 'rewrite_product_base_tag', 10, 0 );
It works fine except it shows products even if I use any random slug.它工作正常,除了它显示产品,即使我使用任何随机的蛞蝓。 The following URLs will show the product based on its name (every single URL shows the "product-name" product):以下 URL 将根据产品名称显示产品(每个 URL 都显示“产品名称”产品):
I also tried to use different regex to limit available slugs.我还尝试使用不同的正则表达式来限制可用的 slug。 It only shows the second option (category_name) properly.它只正确显示第二个选项(category_name)。 The first one (default_slug) shows the Archive page.第一个(default_slug)显示存档页面。 It works exactly the same even if those are changed with each other: the first one shows the Archive page, the second one works fine.即使它们相互更改,它的工作方式也完全相同:第一个显示存档页面,第二个工作正常。
Adding a rewrite_rule function doesn't work at all.添加 rewrite_rule function 根本不起作用。
function rewrite_product_base_tag() {
add_rewrite_tag( '%type%', '(default_slug|category_name)', 'in-type=' );
}
add_action( 'init', 'rewrite_product_base_tag', 10, 0 );
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
function add_custom_attributes_to_permalink( $post_id, $post, $update ) {
//make sure we are only working with Products
if ($post->post_type != 'product' || $post->post_status == 'auto-draft') {
return;
}
//get the product
$_product = wc_get_product($post_id);
//get the "clean" permalink based on the post title
$clean_permalink = sanitize_title( $post->post_title, $post_id );
//next we get all of the attribute slugs, and separate them with a "-"
$attribute_slugs = array(); //we will be added all the attribute slugs to this array
foreach ($_product->get_attributes(); as $attribute_slug => $attribute_value) {
$attribute_slugs[] = $attribute_value;
}
$attribute_suffix = implode('-', $attribute_slugs);
//then add the attributes to the clean permalink
$full_permalink = $clean_permalink.$attribute_suffix;
// unhook the save post action to avoid a broken loop
remove_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
// update the post_name (which becomes the permalink)
wp_update_post( array(
'ID' => $post_id,
'post_name' => $full_permalink
));
// re-hook the save_post action
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.