【问题标题】:Convert GET variables from url to path using add_rewrite_rule in Wordpress使用 Wordpress 中的 add_rewrite_rule 将 GET 变量从 url 转换为路径
【发布时间】:2020-04-16 03:55:12
【问题描述】:

在我的 wordpress 网站(nginx 托管)中,我有一个带有此 url 的产品页面:

https://www.officeshop.co.il/product/my-product/

我开发了一些功能,允许使用 get 参数查看不同的产品型号:

https://www.officeshop.co.il/product/my-product/?build=product-572

我想重写这个 url 使它看起来像 SEO 友好:

https://www.officeshop.co.il/product/my-product/build/product-572

我知道函数 add_rewrite_rule 可以完成这项工作,但在我的情况下我应该传递哪些参数?

【问题讨论】:

    标签: wordpress nginx url-rewriting


    【解决方案1】:

    您可以将以下代码用于您主题的功能。php 使您的链接 SEO 友好。

    add_filter( 'post_type_link', 'change_product_links', 10, 2 );
    function change_product_links( $link, $post) {
        if ( $post->post_type == 'tribe_events' && tribe_event_in_category('product') ) {
            $link = trailingslashit( home_url('/product/' . $post->post_name  ) );
        }
        return $link;
    }
    add_action( 'init', 'product_rewrite_rule', 5);
    function product_rewrite_rule() {
            add_rewrite_rule( '^product/([^/]+)/?', 'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]', 'top' );
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2013-07-06
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多