skip to Main Content

Update:

To clarify my answer.
Problem is that I have YoastSEO plugin and that plugin is responsible for generating sitemap. What I want is to redirect second and third level subcategories (listed in sitemap) to corresponding top level category with url structure as specified.

What I want to is to generate custom url for wp post categories.
Example:
Categories structure is as following:

Cat1
 |_Cat2
    |_ Cat3

WordPress generates following url structure for categories:
//host/category/cat1/cat2/cat3

but what I want to achieve is following:n
//host/category/cat1/?l2cat=cat2&l3cat=cat3

any help is welcome and appriciated

3

Answers


  1. Chosen as BEST ANSWER
    function term_link_filter( $url, $term, $taxonomy ) {       
            $chunks = array_filter(explode('/', $url));
            $cat =  home_url() . '/' . $taxonomy . '/' . $chunks[4];
            $cat = $chunks[5] ? add_query_arg( 'l2cat',  $chunks[5], $cat ) : $cat;
            $cat = $chunks[6] ? add_query_arg( 'l3cat',  $chunks[6], $cat ) : $cat;
            return $cat;
    }
    
    add_filter('term_link', 'term_link_filter', 10, 3);
    

    Note: Problem is that I have YoastSEO plugin and that plugin is responsible for generating sitemap. What I want is to redirect second and third level subcategories to corresponding top level category with url structure as specified.


  2. So the URL you’re trying to achieve is actually going to the "cat1" page as far as WP is concerned. Adding Cat2 and Cat3 as query parameters means that you will have to handle these yourself when the page loads, you’re not on the page that represents "cat3" anymore. In your first URL, "cat1" and "cat2" are just part of the URL – you’re actually ON "cat3". You can certainly do that if that’s what you’re intending…

    What you would want to do is add_query_var for cat2 and cat3. Then in the template that shows cat1 – you can change the content displayed by getting those terms.

    I would caution against it… I’m not an SEO expert, but what you now have is a page with a single canonical URL that actually represents 3 completely different terms based on the query parameters.

    Maybe I misunderstood the question? But the URL structure you outline above looks like what you’re trying to achieve is some kind of archive page that filters by several categories, and not the actual URL to the cat3 term display, which your first URL represents.

    Login or Signup to reply.
  3. solution. Not elegant.

    /* Generate sub categories redirect logic */
    function subcategories_redirect() {
      /* Example
         '/category/first_level_cateogry/second_level_category/third_level_category'
         => '/category/first_level_category?l2cat=second_level_category&l3cat=third_level_category',
      */
    
      $get_top_categories = get_top_categories();
      $category_array = array();
    
      foreach( $get_top_categories as $category ) {
        $args = array(
          'hide_empty' => 0,
          'child_of' => $category->term_id,
          'taxonomy' => 'category'
        );
        $term_children = get_categories($args);
        foreach ( $term_children as $child ) {
          // Cheap and dirty
          $category_ancestors = get_ancestors($child->term_id, 'category');
          $level_2_category = $level_3_category  = "";
          if (count($category_ancestors) == 1 ) {
            $level_2_category = '?l2cat=' . $child->slug;
          } elseif (count($category_ancestors) == 2) {
            $tmp = get_category($category_ancestors[0]);
            $level_2_category = '?l2cat=' . $tmp->slug;
            $level_3_category  = '&l3cat=' . $child->slug;
          }
          $category_array[] = array( ($child->slug) => ('/category/' . $category->slug . '/' . $level_2_category . $level_3_category) );
        }
      }
    
      $flat_category_array = array_merge(...$category_array);
      foreach( $flat_category_array as $category => $url ){
        if( is_category( $category ) ) {
          $url = site_url( $url );
          wp_safe_redirect( $url, 301 );
          exit();
        }
      }
    }
    
    add_action('template_redirect', 'subcategories_redirect');
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search