【问题标题】:Use the category name instead of category slug in permalinks在永久链接中使用类别名称而不是类别 slug
【发布时间】:2013-04-05 20:07:32
【问题描述】:

如何创建一个以下列方式结束的永久链接结构:

/category-name/post-name/

代替:

/category-slug/post-name/

默认情况下,Wordpress 提供%category% 标签结构为"A sanitized version of the category name (category slug field on New/Edit Category panel"

如何在永久链接中使用类别名称而不是类别 slug?

【问题讨论】:

    标签: wordpress mod-rewrite categories slug permalinks


    【解决方案1】:

    【讨论】:

      【解决方案2】:
      add_filter('rewrite_rules_array', 'category_name_rewrite_rule');
      function category_name_rewrite_rule($rules) {
          $new_rules = array();
          $categories = get_categories();
          foreach ($categories as $category) {
              $cat_name = preg_replace('#\s+#', '-', $category->name);
              $new_rules['/'.$category->slug}.'/'] = '/'.url_encode($cat_name).'/';
          }
          return $new_rules + $rules;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        相关资源
        最近更新 更多