【问题标题】:Redirect To Sub Folder By Country按国家/地区重定向到子文件夹
【发布时间】:2017-06-07 15:47:39
【问题描述】:

我正在尝试将我网站的商店部分分为美国和英国部分,因为我向每个国家/地区提供不同的产品。

我想使用一个子文件夹,即。 /us/uk,但仅当客户进入商店部分时(点击商店、类别或产品)。

我认为最好的方法可能是创建一个shop. 子域,并在他们进入shop. 子域后激活基于 IP 的重定向到相应的子文件夹。

我可以使用国家/地区选择器弹出窗口(无需通过 IP 重定向),但我不希望有两个单独的网站,也不希望我的博客帖子位于 /us 或 @987654326 下@ 子文件夹。

这就是为什么我只想在用户点击shop. 部分时激活重定向。

我正在使用 Worpress,所以如果有任何可以使用的插件,我可以使用它们。

如果我必须只使用子文件夹(/shop/uk/product/uk 等),那么我可以这样做,但我找不到任何适用于我的特定要求的建议。

【问题讨论】:

    标签: wordpress .htaccess redirect subdomain subdirectory


    【解决方案1】:

    我会创建一个自定义帖子类型“商店”,然后创建一个自定义分类。 然后为每个国家/地区添加新的自定义分类并将其分配给自定义帖子类型“商店”。

    在functions.php中注册您的自定义帖子类型,如下所示:

    add_action( 'init', 'custom_post_types', 0 );
    function custom_post_types() {
    
    //shops
        register_post_type( 'shops',
            array(
                'labels' => array(
                    'name' => __( 'Shops' ),
                    'singular_name' => __( 'Shops' ),
                    'all_items'  => __( 'All Shops'),
                ),
                'capabilities' => array(
                    'edit_post'          => 'update_core',
                    'read_post'          => 'update_core',
                    'delete_post'        => 'update_core',
                    'edit_posts'         => 'update_core',
                    'edit_others_posts'  => 'update_core',
                    'delete_posts'       => 'update_core',
                    'publish_posts'      => 'update_core',
                    'read_private_posts' => 'update_core'
                ),
                'taxonomies' => array('countries'),
                'menu_position' => 5,
                'public' => true,
                'has_archive' => false,
                'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
                "rewrite" => array(
                    "slug"=>'news/%countries%',
                    "with_front" => false
                ),
            )
        );
    }
    

    然后注册您的自定义分类:

    add_action( 'init', 'build_taxonomies', 0 );
    function build_taxonomies() {
        register_taxonomy('countries', 'shops', array('hierarchical' => true, 'label' => 'Countries', 'query_var' => true, 'rewrite' => array( 'slug' => 'shops', 'with_front' => false)));
     }
    

    为了在注册自定义帖子类型时在重写规则中使用 %countries%/,您需要告诉 WordPress 这意味着什么。你这样做:

    add_filter( 'post_type_link', 'wpa_shops_permalinks', 1, 2 );
    function wpa_shops_permalinks( $post_link, $post ){
        if ( is_object( $post ) && $post->post_type == 'shops' ){
            $terms = wp_get_object_terms( $post->ID, 'countries' );
        if( $terms ){
            return str_replace( '%countries%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多