How to redirect all Product Sub Categories to its Main category Page respectively.

This is a question to find a way.

  • Or you could move all your products into top level categories only. – Andy Macaulay-Brook Jun 22 '18 at 17:02

You will have to use JS for the redirection since header is already sent before we can check the category, following snippet would do the work:

function redirect_to_top_level_parent(){
    if (is_product_category()){
        $cate = get_queried_object(); $cateID = $cate->term_id;
        $parentcats = get_ancestors($cateID, 'product_cat');
        $count = count($parentcats);
        if ($count > 0){
            $count = $count-1;
            $link = get_term_link( $parentcats[$count], 'product_cat' );
            $redirect = "<script>";
            $redirect .= "window.location.replace('{$link}');";
            $redirect .= "</script>";
            echo $redirect;
  • 1
    Great it Worked! – Nimesh Deo Jun 21 '18 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.