Created
July 14, 2022 16:08
-
-
Save dibakarjana/b1f6a0c843533619168e10d7da772577 to your computer and use it in GitHub Desktop.
Wordpress - Redirect any page To Single Post Page or any other variable url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment