如何将 WooCommerce 的产品链接改成 ID 数字形式。

如何将 WooCommerce 的产品链接改成 ID 数字形式。

对于 WordPress 来说,如果是需要调整文章的固定连接那么可以直接修改,但对于 WooCommerce 来说却没有这个选项,需要进行手动操作。

不是英语语系的用户,也就是比如我们中文用户,输入产品标题后显示的链接就是中文字符,而且又长又杂乱,对 SEO 不是很友好。

将下面的代码添加到已激活主题的 `functions.php` 文件中。

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 home_url( 'product/' . $post->ID );
    } else {
        return $link;
    }
}

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' );
}

或者使用 Product ID Permalink for WooCommerce 插件 :

Product ID Permalink for WooCommerce

两种方式都是看个人喜好,插件测试过可用,代码也是正常的。

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者小新

大象、大象 ,你的鼻子怎么那么长 ,妈妈说鼻子长才是漂亮 ......