通过add_url_rewrite从自定义taxanomy和自定义帖子类型中删除slug

时间:2016-07-13 07:20:37

标签: php wordpress .htaccess url-rewriting custom-post-type

问题:

有一个名为product_category的自定义taxanomy和自定义帖子类型作为产品的slug。现在网址看起来像这样。

example.com/categories/laptop

和产品发布

example.com/product/laptop/acer/acerlaptopname

现在我希望他们这样展示:

example.com/laptop

和类似的

example.com/laptop/acer/acerlaptopname

尝试解决方案:

  1. 使用.htaccess的解决方案:
  2. 由于内部wordpress url重写规则,此解决方案无法正常工作。

    1. 解决方案尝试使用add_url_rewriting:
    2. 我在functions.php中添加了以下代码,然后刷新了

      在第一部分,我添加了这段代码并找到了

      example.com/laptop/acer/acerlaptopname    --> as needed
      

      example.com/product/laptop/acer/acerlaptopname  --> not needed
      

      两者都在运行。

      
      
      add_action('init', function()
      {	
      
      	
          add_rewrite_rule('^([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=product&product_category=$matches[1]&product_brand=$matches[2]&name=$matches[3]', 'top');
      }, 0, 0);
      
      ?>
      
      
      

      现在要从cutom taxanomy页面中删除类别slug,我在上面的代码中添加了以下行。

      add_rewrite_rule('(.+)/?$', 'index.php?product_category=$matches[1]', 'top');
      

      这有助于我删除slug"类别"现在这个网址正在运行

      example.com/laptop  --> as needed
      

      example.com/categories/laptop --> this gives 404 as required
      

      我需要的解决方案

      我只想要

      example.com/laptop  --> this to work
      
      example.com/laptop/acer --> this to work
      
      example.com/laptop/acer/laptopname --> this to work
      

      注意:如果您需要任何其他信息,请在下注前点评。

0 个答案:

没有答案
相关问题