I was doing some research on wordpress tags and categories and the below script which is needed for redirecting single posts related categoies and tags to be redirected to the post itself in wordpress. Below code is wordpress redirect without plugin needed. it does not add more overhead on server performance.
Table of Contents
Below code can be added to child theme(recommended) or to your theme’s functions.php file
function redirect_to_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_redirect( $post_url );
}
} add_action(‘template_redirect’, ‘redirect_to_post’);
Note: Child theme’s are safe in WordPress to be sure not to break anything all at once when doing customization.
Above can also used to redirect all posts of category to any single page etc… its just needs to be changed accordingly at $post_url line. Please let me know if you need more help on this.
If you need more of these snippets like below for customization then comment and i will post shortly but comment if needed urgently.
- wordpress redirect page to external url,
- wordpress redirect hook, wordpress redirect plugin
- redirect wordpress site to another url,
- wordpress 301 redirect,
- wordpress 301 redirect enabled,
- wordpress page template redirect,
- wordpress redirect referrer,
- wordpress redirect to new domain
- wordpress redirect to another page
- wordpress redirect http to https
- wordpress redirect url
- wordpress redirect after login
- wordpress redirect to https
- wordpress redirect after login based on role
- wordpress redirect all pages to one page
- wordpress redirect admin page
- wordpress redirect after submit comment
- wordpress redirect all pages to homepage
- wordpress redirect a page