Redirect To Single Post Page
This article is about template redirecting in WordPress using template_redirect hook. This is a very simple but very handy for a WordPress blogger who wants to redirect the user to the single post template/full post view (even if there is only index.php) when there is only one post in the archive/category/tag. It’s helpful to a new blog site when there is a few articles like mine. I’ve found this handy and using it on my site, so I think someone else will like it too. Simply paste this code snippet in the functions.php and you are done.
function redirect_to_single_post(){ global $wp_query; if((is_archive() || is_category()) && $wp_query->post_count == 1){ // Setup post data the_post(); // Get permalink $post_url = get_permalink(); // Redirect to single post page wp_redirect( $post_url ); } } add_action('template_redirect', 'redirect_to_single_post');