Skip to content

Instantly share code, notes, and snippets.

@andrii-kvlnko
Last active May 3, 2021 07:38
Show Gist options
  • Save andrii-kvlnko/77e96072b7cef7c56f4902e0c41267ee to your computer and use it in GitHub Desktop.
Save andrii-kvlnko/77e96072b7cef7c56f4902e0c41267ee to your computer and use it in GitHub Desktop.
WordPress - Template redirect
function prefix_url_rewrite_templates() {
    if ( get_query_var( 'photos' ) && is_singular( 'news' ) ) {
        add_filter( 'template_include', function() {
            return get_template_directory() . '/single-news-image.php';
        });
    }
    if ( get_query_var( 'videos' ) && is_singular( 'news' ) ) {
        add_filter( 'template_include', function() {
            return get_template_directory() . '/single-news-video.php';
        });
    }
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment