如何將 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

兩種方式都是看個人喜好,插件測試過可用,代碼也是正常的。

文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者小新

大象、大象 ,你的鼻子怎麼那麼長 ,媽媽說鼻子長才是漂亮 ......