当前位置:首页-WordPress文章-WooCommerce-正文

修改Woocommerce商品固定链接其它自定义文章类型适用

修改Woocommerce商品固定链接其它自定义文章类型适用 - 第1张在更新Concise主题时集成了前端用户中心和商城插件,发现插件中为商城产品文章设置好了固定链接,所以把4166am金沙信心之选的作品集与商城的两种自定义类型的文章固定链接添加上。

自定义文章类型固定链接可设置两形式,分别为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. }

两种形式的固定链接代码也只是设置了显示方式以及显示的内容,ID就显示0-9其中的数字,别名就显示所有的字符。

使用固定链接后,Purity主题中集成的作品集文章类型在文章页面获取作品集分类名称和SEO中获取产品关键字时获取不了,不使用固定是正常,所以Google找到应对方案。

1、作品集文章页面获取分类名称,添加到作品集文章页面中

  1. $terms = get_the_terms($post->ID, 'portfolio_field' );//portfolio_field为作品集分类法
  2. if ($terms && ! is_wp_error($terms)) :
  3.     $term_names_arr = array();
  4.     foreach ($terms as $term) {
  5.         $term_names_arr[] = $term->name;
  6.     }
  7.     $terms_name_str = join( ","$term_names_arr);
  8. endif;
  9. ?>

2、使用以下代码来调用分类名称

  1. echo $terms_name_str; ?>

通过上面的代码我们可以获取portfolio_field的分类名称,获取产品关键字名称也同样适用,将上面代码中的portfolio_field修改为product_tag即可。

[successbox]设置好自定义文章类型的固定链接,看着确实很舒服,也有利于SEO,同时也解决设置固定链接后与其它代码产生的冲突。[/successbox]

本文原创,作者:萨龙龙,其版权均为4166am金沙信心之选所有。
如需转载,请注明出处:/custom-post-type-permalink.html

\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u56fa\u5b9a\u94fe\u63a5\u53ef\u8bbe\u7f6e\u4e24\u5f62\u5f0f\uff0c\u5206\u522b\u4e3aID\u548c\u522b\u540d\uff0c\u8428\u9f99\u9f99\u8ba4\u4e3a\u4f7f\u7528\u522b\u540d\u7684\u65b9\u5f0f\u518d\u52a0\u9002\u2026@\u8428\u9f99\u7f51\u7edc&appkey=746468751&pic=https:\/\/salongweb.com\/wp-content\/uploads\/2015\/07\/custom-post-type-permalink.jpg&searchPic=true","qq":"https:\/\/connect.qq.com\/widget\/shareqq\/index.html?url=https:\/\/salongweb.com\/custom-post-type-permalink.html&title=\u4fee\u6539Woocommerce\u5546\u54c1\u56fa\u5b9a\u94fe\u63a5\u5176\u5b83\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u9002\u7528&pics=https:\/\/salongweb.com\/wp-content\/uploads\/2015\/07\/custom-post-type-permalink.jpg&summary=\u5728\u66f4\u65b0Concise\u4e3b\u9898\u65f6\u96c6\u6210\u4e86\u524d\u7aef\u7528\u6237\u4e2d\u5fc3\u548c\u5546\u57ce\u63d2\u4ef6\uff0c\u53d1\u73b0\u63d2\u4ef6\u4e2d\u4e3a\u5546\u57ce\u4ea7\u54c1\u6587\u7ae0\u8bbe\u7f6e\u597d\u4e86\u56fa\u5b9a\u94fe\u63a5\uff0c\u6240\u4ee5\u628a\u8428\u9f99\u7f51\u7edc\u7684\u4f5c\u54c1\u96c6\u4e0e\u5546\u57ce\u7684\u4e24\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u6587\u7ae0\u56fa\u5b9a\u94fe\u63a5\u6dfb\u52a0\u4e0a\u3002

\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u56fa\u5b9a\u94fe\u63a5\u53ef\u8bbe\u7f6e\u4e24\u5f62\u5f0f\uff0c\u5206\u522b\u4e3aID\u548c\u522b\u540d\uff0c\u8428\u9f99\u9f99\u8ba4\u4e3a\u4f7f\u7528\u522b\u540d\u7684\u65b9\u5f0f\u518d\u52a0\u9002\u2026&site=\u8428\u9f99\u7f51\u7edc","zone":"https:\/\/sns.qzone.qq.com\/cgi-bin\/qzshare\/cgi_qzshare_onekey?url=https:\/\/salongweb.com\/custom-post-type-permalink.html&title=\u4fee\u6539Woocommerce\u5546\u54c1\u56fa\u5b9a\u94fe\u63a5\u5176\u5b83\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u9002\u7528&desc=\u5728\u66f4\u65b0Concise\u4e3b\u9898\u65f6\u96c6\u6210\u4e86\u524d\u7aef\u7528\u6237\u4e2d\u5fc3\u548c\u5546\u57ce\u63d2\u4ef6\uff0c\u53d1\u73b0\u63d2\u4ef6\u4e2d\u4e3a\u5546\u57ce\u4ea7\u54c1\u6587\u7ae0\u8bbe\u7f6e\u597d\u4e86\u56fa\u5b9a\u94fe\u63a5\uff0c\u6240\u4ee5\u628a\u8428\u9f99\u7f51\u7edc\u7684\u4f5c\u54c1\u96c6\u4e0e\u5546\u57ce\u7684\u4e24\u79cd\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u6587\u7ae0\u56fa\u5b9a\u94fe\u63a5\u6dfb\u52a0\u4e0a\u3002

\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u56fa\u5b9a\u94fe\u63a5\u53ef\u8bbe\u7f6e\u4e24\u5f62\u5f0f\uff0c\u5206\u522b\u4e3aID\u548c\u522b\u540d\uff0c\u8428\u9f99\u9f99\u8ba4\u4e3a\u4f7f\u7528\u522b\u540d\u7684\u65b9\u5f0f\u518d\u52a0\u9002\u2026&summary=&site=\u8428\u9f99\u7f51\u7edc"}>