If you've stumbled upon this post it most likely means that you're running a WordPress blog with a lot of archive pages (categories and/or tags) containing a single post. Archive pages in WordPress are those automatically-generated pages that appear when the user clicks on a category or tag, showing a paged list of all the posts that contain that tag or are related to that category.
These lists are useful when they contain a lot of posts; however, whenever there's only a single relevant post, it would arguably be better to directly bring the user to the pertaining article, skipping the listing page.
All you have to do to enforce such behaviour is to add the following source code snippet to your WordPress installation's function.php file:
1 2 3 4 5 6 7 8 9 10 |
/// RYADEL ADDED: 302-redirect single-post archives (tags or categories) to the relevant post function redirect_single_post_archives_to_post(){ global $wp_query; if (is_archive() && $wp_query->post_count == 1 ){ the_post(); $post_url = get_permalink(); wp_redirect($post_url, 302); } } add_action('template_redirect', 'redirect_single_post_archives_to_post'); |
In order to do that, go to WordPress Administration panel, then navigate through Appearance > Theme Editor: from there, select the function.php file from the list to the right.
That's about it: I hope that this snippet will be useful for those who want to enforce such behaviour in their WordPress website!