删除产品类别插件适用于%1子类别而不是%2子类别

时间:2017-02-26 作者:Bas

我正在使用删除产品类别slug插件与1个子类别一起使用,而不是与2子类别一起使用。例如:

www.domain.com/europe/netherlands/amsterdam (doesn\'t work)
www.domain.com/product-categorie/europe/netherlands/amsterdam (work)
www.domain.com/europe/netherlands           (work)
www.domain.com/product-categorie/europe/netherlands           (work)
插件代码:

/*
Plugin Name: Remove product-category slug
Plugin URI: https://timersys.com/
Description: Check if url slug matches a woocommerce product category and use it instead
Version: 0.1
Author: Timersys
License: GPLv2 or later
*/
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;
});
我怎样才能做到这一点?

2 个回复
最合适的回答,由SO网友:Bas 整理而成

在我的设置中,我知道最多有3个参数,所以这是我的解决方案

add_filter(\'request\', function( $vars ) {
    global $wpdb;

    if(isset($vars[\'error\'])){
        $lastVar = basename($_SERVER[\'REQUEST_URI\']);
        $vars[\'attachment\'] = $lastVar;
    }
    ........rest off code........
}

SO网友:Bogdean

如果有人仍在搜索此问题。。。将帖子的永久链接设置更改为;物品名称“;(site.com/name-article)为我修复了这个问题。

我也在函数中使用此代码。php:

add_filter(\'request\', function( $vars ) {
    global $wpdb;

    if(isset($vars[\'error\'])){
        $lastVar = basename($_SERVER[\'REQUEST_URI\']);
        $vars[\'attachment\'] = $lastVar;
    }
   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;
});

add_filter(\'term_link\', \'term_link_filter\', 10, 3);
function term_link_filter( $url, $term, $taxonomy ) {
    $url=str_replace("/./","/",$url);
     return $url;
}

相关推荐

更新页面(update-core.php)和插件页面(plugins.php)恢复到主页

我在Wordpress网站的管理视图中收到通知,我有一个网站和插件的可用更新(在我的网络管理仪表板中,在“插件”和“更新”旁边的红色圆圈中有“1”)。。。但当我尝试同时转到“更新”页和;插件页面,是否显示主页?此时的URL为http:///wp-admin/network/update-core.php/和http:///wp-admin/plugins.php/分别地因此,我永远无法到达真正的更新页面,也无法更新我的Wordpress或插件。如何显示更新或插件页面?