• Resolved choette

    (@choette)


    I tried various plugins to remove the category base from my category archive URLs:

    Yoast SEO
    Remove Category URL
    Remove Category Base

    Using “Remove Category URL” or “Remove Category Base” the URL is indeed changed as expected, “category” is stripped from the URL. But unfortunately the menu links to those archive pages still include the category base, therefore returning a 404 error when clicked.

    Any idea what I can do?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    This is not that simple… In order to do this with multilanguage you need to add rewrite rules for every translation of every categories. This snippet should do it:

    
    add_filter('category_rewrite_rules', function($rules) {
      global $wp_rewrite, $sublanguage_admin;
      
      if (isset($sublanguage_admin)) {
        
        $terms = get_terms(array(
          'taxonomy' => 'category',
          'hide_empty' => false
        ));
      
        $term_slugs = array();
      
        foreach ($sublanguage_admin->get_languages() as $language) {
        
          if ($sublanguage_admin->is_sub($language)) {
        
            $taxonomy_slug = $sublanguage_admin->translate_taxonomy('category', $language, 'category');
        
            foreach ($terms as $term) {
          
              $term_slugs[] = $sublanguage_admin->translate_term_field($term, 'category', 'slug', $language, $term->slug);
          
            }
        
            $term_slug_group = implode('|', $term_slugs);
        
            $rules["({$term_slug_group})/{$wp_rewrite->pagination_base}/?([0-9]{1,})/?$"] = 'index.php?sublanguage_slug='.$taxonomy_slug.'&category_name=$matches[1]&paged=$matches[2]';
            $rules['('.$term_slug_group.')/?$'] = 'index.php?sublanguage_slug='.$taxonomy_slug.'&category_name=$matches[1]';
        
          }
        
        }
        
      }
    
      return $rules;
    
    }, 11);
    

    Then you can fix the category links this way:

    
    add_filter('term_link', function($termlink, $term, $taxonomy) {
      global $wp_rewrite, $sublanguage;
      
      $permastruct = $wp_rewrite->get_extra_permastruct($taxonomy);
      
      if (isset($sublanguage) && $permastruct) {
      
        $translated_tax = $sublanguage->translate_taxonomy($taxonomy, null, $taxonomy);
        $translated_slug = $sublanguage->translate_term_field($term, $taxonomy, 'slug');
      
        $termlink = str_replace(array($taxonomy, "%$translated_tax%"), array($translated_tax, $translated_slug), $permastruct);
        $termlink = home_url(user_trailingslashit($termlink, 'category'));
        
      }
      
      return $termlink;
    }, 11, 3);
    

    Tell me if it works. Don’t forget to refresh your permalinks!

    Thread Starter choette

    (@choette)

    Hi Maxime,
    thank you very much!
    The above snippets work perfectly in combination with the Remove Category URL plugin.

    Thanks again for the fantastic support!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Removing category base’ is closed to new replies.