当前位置:首页>>WordPress教程

删除woocommerce产品类别中URL中的product-category

通常我们在后台设置好产品类别后,前台显示为www.a.com/product-category/name,那么如何让它显示为www.a.com/name?将以下代码放在主题文件functions.php,之后设置链接规则Product category base为一个小数点.即可。add_filter('request', function( $vars ) { global $wpdb; if( ! empt

admin

通常我们在后台设置好产品类别后,前台显示为www.a.com/product-category/name,那么如何让它显示为www.a.com/name?

将以下代码放在主题文件functions.php,之后设置链接规则Product category base为一个小数点.即可。

add_filter('request', function( $vars ) {

   global $wpdb;

   if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) {

      $slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) );

      $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug )));

      if( $exists ){

         $old_vars = $vars;

         $vars = array('product_cat' => $slug );

         if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) )

            $vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page'];

         if ( !empty( $old_vars['orderby'] ) )

            $vars['orderby'] = $old_vars['orderby'];

         if ( !empty( $old_vars['order'] ) )

            $vars['order'] = $old_vars['order']; 

      }

   }

   return $vars;

});


{dede:include filename="menu.htm"/}