Skip to content

Instantly share code, notes, and snippets.

@JoeSz
Forked from alexwcoleman/functions.php
Created December 9, 2020 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeSz/01ec83dbcac8e343342b5168e83da690 to your computer and use it in GitHub Desktop.
Save JoeSz/01ec83dbcac8e343342b5168e83da690 to your computer and use it in GitHub Desktop.
WordPress: Redirect single custom post type to archive page.
// 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