WooCommerce:只有产品名称中的数字 - 问题 [英] WooCommerce: only numbers in product name - issue
问题描述
我有一个使用 WooCommerce 的网站.
I have a website using WooCommerce.
我发现了一个问题:如果产品名称仅包含数字 - 您无法查看此产品.浏览器正在显示类似类别"页面的内容.
I have found an issue: if product name including only numbers - you can't view this product. Browser is showing something like Category page.
如果我在 slug 中添加文字字符 - 链接工作正常,但我不能一直这样做.
If I add literal characters into the slug - the link is working correctly, but I can't do that all the time.
我认为此问题会导致已安装的插件之一.我一一禁用了所有插件(WooCommerce 除外),但没有帮助.
I thought that this problem creates one of the installed plugins. I disabled all plugins one by one (except WooCommerce), but it did not help.
我需要如何解决这个问题?
How do I need to fix that?
推荐答案
我创建了一个钩子.
如果只有产品名称中的数字 - 函数会在 Slug 中添加-product".
If only the numbers present in the product name - function adds "-product" to the Slug.
即使您编辑了 slug 并只留下数字 - 该功能将再次起作用
Even if you edit the slug and leave it only numbers - the function will work again
add_filter('wp_insert_post_data', 'edit_slug', 10);
function edit_slug($data) {
global $post_ID;
if (!empty($data['post_name']) && $data['post_status'] == "publish" && $data['post_type'] == "product") {
if (ctype_digit($data['post_name'])) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . 'product';
}
}
return $data;
}
这篇关于WooCommerce:只有产品名称中的数字 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!