Woocommerce修改商品固定链接并添加html后缀

自定义文章类型固定链接可设置两形式,分别为ID和别名,个人认为使用别名的方式再加适合SEO,不过两种方式代码都已经放上。

别名固定方式

  1. $posttypes = array(
  2. 'product' => 'product',//Woocommerce产品自定义文章类型
  3. 'portfolio' => 'portfolio'//作品集自定义文章类型
  4. );
  5. add_filter('post_type_link', 'custom_book_link', 1, 3);
  6. function custom_book_link( $link, $post = 0 ){
  7. global $posttypes;
  8. if ( in_array( $post->post_type,array_keys($posttypes) ) ){
  9. return home_url( $posttypes[$post->post_type].'/' . $post->post_name .'.html' );
  10. } else {
  11. return $link;
  12. }
  13. }
  14. add_action( 'init', 'custom_book_rewrites_init' );
  15. function custom_book_rewrites_init(){
  16. global $posttypes;
  17. foreach( $posttypes as $k => $v ) {
  18. add_rewrite_rule(
  19. $v.'/([一-龥a-zA-Z0-9_-]+)?.html([sS]*)?$',
  20. 'index.php?post_type='.$k.'&name=$matches[1]',
  21. 'top' );
  22. }
  23. }

ID固定形式

  1. $posttypes = array(
  2. 'product' => 'product',//Woocommerce产品自定义文章类型
  3. 'portfolio' => 'portfolio'//作品集自定义文章类型
  4. );
  5. add_filter('post_type_link', 'custom_book_link', 1, 3);
  6. function custom_book_link( $link, $post = 0 ){
  7. global $posttypes;
  8. if ( in_array( $post->post_type,array_keys($posttypes) ) ){
  9. return home_url( $posttypes[$post->post_type].'/' . $post->ID .'.html' );
  10. } else {
  11. return $link;
  12. }
  13. }
  14. add_action( 'init', 'custom_book_rewrites_init' );
  15. function custom_book_rewrites_init(){
  16. global $posttypes;
  17. foreach( $posttypes as $k => $v ) {
  18. add_rewrite_rule(
  19. $v.'/([0-9]+)?.html$',
  20. 'index.php?post_type='.$k.'&p=$matches[1]',
  21. 'top' );
  22. }
  23. }

最后

将上面两段代码选择一种拷贝一下放到主题的functions.php文件中。如下图

完成之后去WordPress后台--设置--固定连接--产品固定连接--自定义base中输入【/%postname%.html/】就可以固定Woocommerce商品连接并进行伪静态了。

喜欢就支持以下吧
倾尘SEO
  • 本文由 倾尘SEO 发表于 2017年4月22日
  • 转载请务必保留本文链接:https://www.qcwlseo.com/woo-html-zhouzhui.html
 评论   1   访客   1
    • cmssky
      cmssky

      这种方法在现在版本已经不适用了,楼主能更新下代码吗?感谢!