Skip to content

Snippet: Define a Dynamic Template for Posts that are Part of a Post Series

The following snippet can be used to set a dynamic template for any post that is part of a Post Series. This will allow you to create a different template to use for these posts.

/**
 * Define a custom template to use with posts that are part of a post series.
 */
add_filter( 'wpex_singular_template_id', function( $template_id ) {
	if ( ! is_singular( 'post' ) ) {
		return $template_id; // only apply to the standard post post type.
	}
	if ( taxonomy_exists( 'post_series' ) ) {
		$post_series = get_the_terms( get_post(), 'post_series' );
		if ( ! is_wp_error( $post_series ) && $post_series ) {
			$template_id = 'YOUR_TEMPLATE_ID_HERE'; // !!! EDIT THIS !!!
		}
	}
	return $template_id;
} );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top