如何将Woocommerce产品链接设置为产品ID并添加html后缀

分享一种把Woocommerce的产品ID作为产品URL基础的方法。

使用Woocommerce添加产品,产品的固定链接(Product permalinks)的设置形式有默认(Default)、商店基础链接(Shop base)、添加商店和分类目录地址(Shop base with category)和自定义基础链接(Custom base)四种形式。

但是如果想要使用自义定基础链接,比如将产品链接设置为产品ID并添加html后缀(URL显示为:www.zhiwimao/product/123.html),如果我们仅在自定义基础链接填写规则:/product/%post_id%.html/ 是不够的。还需要输入自定义base,才能生效。

如何将Woocommerce产品链接设置为产品ID并添加html后缀

如果你也想要类似的URL结构,可以使用以下这段代码来实现。

add_filter(‘post_type_link’, ‘wpse33551_post_type_link’, 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type != ‘product’ )
return $link;

return home_url( ‘product/’ . $post->ID . ‘.html’ );
}

add_action( ‘init’, ‘wpse33551_rewrites_init’ );
function wpse33551_rewrites_init(){
add_rewrite_rule(
‘product/([0-9]+)?.*?$’,
‘index.php?post_type=product&p=$matches[1]’,
‘top’
);
}

将代码添加到外观→主题文件编辑器→模板函数(function.php)中,就可以实现了.

需要注意的是代码中的设置要和自定义基础链接设置保持一致。

原创文章,作者:华再,如若转载,请注明出处:https://www.zhiwaimao.com/woocommerce-product-id-in-url/

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注