1

I am looking for a way to redirect only the first category archive page to a normal page I have created.

I am using this code but this redirects all pages of the category archive. I only want the first page of the category archive to be redirected.

function my_page_template_redirect()
{
    if ( is_category( 'news-articles' ) ) {
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );
  • When you say first page, do you mean just the parent category should redirect while sub categories should not - or do you mean /news-articles/ should redirect while /news-articles/page/2/ should not? – Xhynk Mar 8 '18 at 0:18
  • I mean /news-articles/ should redirect while /news-articles/page/2/ should not... – Joanna Mikalai Mar 8 '18 at 0:24
  • Gotcha, just wanted to make sure because the answers would quite a bit. You just need to check the current paged values. I've provided 2 solutions, I'd recommend using is_paged() – Xhynk Mar 8 '18 at 0:27
  • ok makes sense.. now I have 8 different categories.. should I create a separate function for each, or is there a better way to go about this? – Joanna Mikalai Mar 8 '18 at 0:32
  • According to the function reference for is_category() it takes $mixed values, even an array of category names - I've updated the answer to reflect an array – Xhynk Mar 8 '18 at 0:39
1

If you want paginated pages to not redirect, you can check the is_paged() boolean. It will return true if you're on an archive page greater than page 1 ( the first page of the archive ). So if it returns false, it means we're on the page you want redirected.

function my_page_template_redirect(){
    $category_array = array(
        'news-articles',
        'category-2',
        'category-3',
           //...
        'category-8'
    );

    if( is_category( $category_array ) && !is_paged() ){
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Alternatively, you can check the current paged value with get_query_var(), which would look like:

function my_page_template_redirect(){
    if( is_category( 'news-articles' ) && get_query_var( 'paged' ) == 0 ){
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

If your desired URL is different for each category, and you can't get the URL from the category name, then something like this may work for your needs without too much overhead:

function my_page_template_redirect(){
    $category_array = array(
        'news-articles'  => '/news',
        'category-2'     => '/cat-2',
        'third-category' => '/third_category',
          //...
        'NumberEight'    => '/Eight'
    );

    foreach( $category_array as $category => $url ){
        if( is_category( $category ) && !is_paged() ){
            $url = site_url( $url );

            wp_safe_redirect( $url, 301 );
            exit();
        }
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

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.