Skip to content

Define Your Dynamic Post Template via Code

In this article

    Total lets you assign a dynamic template to your single posts, portfolio items, staff members and testimonials directly in the Customizer. If you’re registering custom post types with the Post Types Unlimited plugin, you can set the dynamic template from the post type’s settings as well.

    However, if your custom post types are registered some other way (for example, with custom code or a different plugin), you’ll need to assign the dynamic template using the wpex_get_singular_template_id filter.

    Add the snippet below to your child theme’s functions.php file.

    /**
     * Set a dynamic template for a custom post type.
     */
    add_filter( 'wpex_singular_template_id', function( $template_id ) {
    	if ( is_singular( 'my_post_type' ) ) {
    		$template_id = 123; // Replace with your template ID.
    	}
    	return $template_id;
    } );

    The same filter can be used to conditionally load different templates for the same post type. This is useful if you want certain posts (such as those in a specific category, with a certain tag, or by a particular author) to use a different layout. For example:

    /**
     * Use different dynamic templates for the same post type.
     */
    add_filter( 'wpex_singular_template_id', function( $template_id ) {
    	if ( is_singular( 'my_post_type' ) && has_term( 'my-term', 'my_taxonomy' ) ) {
    		$template_id = 123; // Template for posts in "my-term".
    	}
    	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)
    Related Snippets
    Back To Top