Redirect single post to main category

How can I redirect single post from some categories to main categories?

Assuming your main category url is like “www.website.com/works/”, and that a post url of this category is like “www.website.com/works/wordpress/”, you can add this snippet to your functions.php file:

[php]
/**
* Redirect single post from some categories to main categories?
*/
add_action( ‘template_redirect’, ‘wpster_redirect_single_post’ );
function wpster_redirect_single_post() {
$current_category = get_query_var(‘post_type’);
$category_to_exclude = array(‘works’);
if (is_single() && in_array( $current_category, $category_to_exclude )) {
wp_redirect( home_url( ‘/’.$current_category ), 301 );
exit;
}
}
[/php]

Submit a Comment

Your email address will not be published. Required fields are marked *