代码之家  ›  专栏  ›  技术社区  ›  Erick Martim Arman Malik

有没有办法在Wordpress上创建重复的子类别slug?

  •  3
  • Erick Martim Arman Malik  · 技术社区  · 10 年前

    我疯狂地在网上搜索过这个问题,但似乎找不到一个不太过时的答案,也找不到实际可行的答案。

    我的问题就像这个家伙在这个论坛帖子上描述的一样(来自4,神圣 年前!): https://wordpress.org/support/topic/duplicate-subcategory-slug-problem

    我目前正在使用最新版本Wordpress 4.1,我正在努力实现以下目标:

    假设我的网站上有两个类别 靴子 . 在这两个目录中,我想创建一个名为 红色 。这就是我的类别结构的样子:

     - Boots // -> URL: http://example.com/boots
     | ----- Red // -> URL: http://example.com/boots/red (GREAT!)
    
     - Shoes // -> URL: http://example.com/shoes
     | ----- Red // -> URL: http://example.com/shoes/red-shoes (cmon, wp, nooo !)
    

    我想实现的目标是:

     - Boots // -> URL: http://example.com/boots
     | ----- Red // -> URL: http://example.com/boots/red
    
     - Shoes // -> URL: http://example.com/shoes
     | ----- Red // -> URL: http://example.com/shoes/red
    

    这样会很完美!!!

    如果你想知道,是的,我知道这只是wordpress的一部分,他们不想改变这一点,我理解为什么以及所有的爵士乐,是的。 但对于这个特殊的项目,我真的需要这个。

    我想我可能不得不改变核心,如果我这样做的话,我无法更新Wordpress,我知道,但如果我需要,我对此没有任何问题。

    但如果有另一种方法来构建这个层次结构,它不涉及类别,也不破坏我的URI,我也可以尝试。任何帮助或提示都很感谢,伙计们,非常感谢!

    3 回复  |  直到 10 年前
        1
  •  2
  •   Natiaz    9 年前

    我的分类法也有同样的问题,我刚刚解决了它,希望它能有所帮助。

    首先,你有你的父分类法,当你创建子分类时,你必须确保父蛞蝓被添加到子分类蛞蝓中。在您的示例中,它看起来像:

    • 靴子红色
    • 蓝色靴子
    • 鞋红色
    • 蓝色的鞋子

    这将为每个子类别提供一个唯一的slug。

    现在重要的是,重写规则和分类链接:

    定义分类法时,您应该这样做:

    register_taxonomy('mytaxonomy',array('mycustompost'), array(
        'hierarchical'      => true,
        'sort'              => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'     => array(
          'slug'        => '/%parent%'
        )
      ));
    

    此外,您还必须修改permalink,使其具有所需的结构,如果它是自定义分类法(如我的情况),则使用term_link过滤器进行。我的函数如下所示:

    function mytaxonomy_link( $permalink, $term, $taxonomy )
    {
      if ( $taxonomy !== 'mytaxonomy'  && false === strpos( $permalink, '%parent%' ) )
          return $permalink;
    
      $temp_padre = $term->parent;
      $padre = get_term_by('id', $temp_padre, 'mytaxonomy');
    
    
    
      $cat_name = 'general';
      if(isset($padre) && !empty($padre))
      {
        $cat_name  = $padre->slug; 
        $termslug  = $term->slug;
        $new_slug  = str_replace( $cat_name , '' , $termslug);
        $new_slug  = ltrim($new_slug, '-');
    
        $permalink  = str_replace( '%parent%' , $cat_name , $permalink );
        $permalink  = str_replace( $termslug , $new_slug , $permalink );
    
      }
    
      return $permalink;    
    }
    
    add_filter( 'term_link', 'mytaxonomy_link', 10, 3 );
    

    此函数将子类别链接转换为: http://example.com/boots/red

    重写规则带来了所有的魔力:

      add_rewrite_rule('([^/]+)/([^/]+)/?$', 'index.php?mytaxonomy=$matches[1]-$matches[2]','top');
      add_rewrite_rule('([^/]+)/([^/]+)', 'index.php?mytaxonomy=$matches[1]-$matches[2]','top');
    

    在我的案例中,我的帖子/分类关系要复杂得多,但我认为这个解决方案会对您有所帮助。

    希望这会有所帮助,因为我已经花了很长时间试图找到解决这个问题的方法。

        2
  •  1
  •   ojrask    10 年前

    如果子类别结构的深度不超过 shoes/red (不是 shoes/red/hiking 例如),可以使用 permalink endpoints 以模拟类别。Permalink端点从 domain.com/request/url/?endpoint .

    将每种颜色或类似颜色设置为端点后 EP_CATEGORY ,类别URL可以加后缀 /red , /blue 等等。将端点应用于 EP_ALL_ARCHIVES 允许CPT和日期存档上的端点。

    然后在过滤器或动作中,例如 template_include 可以使用全局 $wp_query 对象并相应地过滤类别项。

    如果 用户向URL输入另一个后缀(例如。 /red/hiking ), $wp_query 将它们识别为键/值对 red => hiking ( ?red=hiking 作为原始查询字符串)( $wp_query -> get( 'red' ); 回报 hiking ). 这可能是好事,也可能是坏事,这取决于您如何实现逻辑。

    添加永久链接端点后,需要使用函数刷新安装的重写规则 flush_rewrite_rules() 或者转到管理面板permalinks管理部分并按一次update。

    注意:不能为端点使用动态名称。您需要为每个变量(红色、蓝色等)定义一个静态端点。

        3
  •  0
  •   Faye D.    3 年前

    在每一个问题中,都有适当的方法来解决它,以及任何其他问题 间接的 方法

    我花了两天时间以正确的方式解决了这一问题,所以我将在这里为将来可能需要它的任何人添加它。

    add_filter('wp_unique_term_slug', 'prevent_cat_suffix', 10, 3);
    function prevent_cat_suffix($slug, $term, $original_slug)
    {
        if ($term->post_type == 'product' && $term->parent !== 0) {
            return $original_slug;
        }
    
        return $slug;
    }
    

    正如您在代码中所看到的,我已经将其缩小到产品,但您也可以通过删除 $term->post_type == 'product' && 在上面的代码中。