Created
September 9, 2017 18:07
-
-
Save alexwcoleman/cc08ff4b4a19b5eb4f42674824cd458f to your computer and use it in GitHub Desktop.
WordPress: Redirect single custom post type to archive page.
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
// redirect single posts to the archive page, scrolled to the current member. | |
// * this is for a post type of "board_member" | |
add_action( 'template_redirect', function() { | |
if ( is_singular('board_member') ) { | |
// I used the two variables to put the member's name as the ID of the article | |
// so the redirect would automatically scroll. This can be removed. | |
$title = get_the_title(); | |
$url_append = str_replace( ' ', '-', strtolower($title) ); | |
global $post; | |
$redirectLink = get_post_type_archive_link( 'board_member' ) . "#" . $url_append; | |
wp_redirect( $redirectLink, 302 ); | |
exit; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use the built in
sanitize_title
in line 8.Use
$url_append = sanitize_title($title);
instead of$url_append = str_replace( ' ', '-', strtolower($title) );