【问题标题】:Change WooCommerce product Shop URL更改 WooCommerce 产品商店 URL
【发布时间】:2019-08-26 05:44:17
【问题描述】:

有没有办法更改店铺网址 来自:https://example.com/shop/productpage/

收件人:https://example.com/productpage/

【问题讨论】:

标签: wordpress woocommerce


【解决方案1】:

使用下面的代码制作一个新插件或添加到主题中的functions.php。记住更新永久链接

function wndev_remove_slug( $post_link, $post ) {
    if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
        return $post_link;
    }
    if('product' == $post->post_type){
        $post_link = str_replace( '/shop/', '/', $post_link );  
    }else{
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wndev_remove_slug', 10, 2 );

function wndev_woo_product_rewrite_rules($flash = false) {
    global $wp_post_types, $wpdb;
    $siteLink = esc_url(home_url('/'));
    foreach ($wp_post_types as $type=>$custom_post) {
        if($type == 'product'){
            if ($custom_post->_builtin == false) {
                $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
                            FROM {$wpdb->posts} 
                            WHERE {$wpdb->posts}.post_status = 'publish' 
                            AND {$wpdb->posts}.post_type = '{$type}'";
                $posts = $wpdb->get_results($querystr, OBJECT);
                foreach ($posts as $post) {
                    $current_slug = get_permalink($post->ID);
                    $base_product = str_replace($siteLink,'',$current_slug);
                    add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');                    
                    add_rewrite_rule($base_product.'comment-page-([0-9]{1,})/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&cpage=$matches[1]', 'top');
                    add_rewrite_rule($base_product.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&feed=$matches[1]','top');
                }
            }
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'wndev_woo_product_rewrite_rules');

function wndev_woo_new_product_post_save($post_id){
    global $wp_post_types;
    $post_type = get_post_type($post_id);
    foreach ($wp_post_types as $type=>$custom_post) {
        if ($custom_post->_builtin == false && $type == $post_type) {
            wndev_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'wndev_woo_new_product_post_save');

【讨论】:

  • 感谢您在重新保存永久链接后的工作。当您拥有超过 10 万种产品时,这会对性能产生重大影响吗?
  • 否。它只更新链接和规则。与产品数量无关
猜你喜欢
  • 2019-03-05
  • 1970-01-01
  • 2016-09-06
  • 2021-05-09
  • 1970-01-01
  • 2020-11-26
  • 2014-05-27
  • 1970-01-01
  • 2017-09-13
相关资源
最近更新 更多