Skip to main content
added 676 characters in body
Source Link
Xhynk
  • 870
  • 5
  • 19

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' );

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 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' );
added 89 characters in body
Source Link
Xhynk
  • 870
  • 5
  • 19

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(){
    if($category_array is_category= 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 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(){
    if( is_category( 'news-articles' ) && !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 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' );
added 88 characters in body
Source Link
Xhynk
  • 870
  • 5
  • 19

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 (so the first page 2 and beyondof the archive ). So if it returns false, it means we're on the page you want redirected.

function my_page_template_redirect(){
    if( is_category( 'news-articles' ) && !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' ) >== 10 ){
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

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 1 (so page 2 and beyond).

function my_page_template_redirect(){
    if( is_category( 'news-articles' ) && !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' ) > 1 ){
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

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(){
    if( is_category( 'news-articles' ) && !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' );
Source Link
Xhynk
  • 870
  • 5
  • 19
Loading