Skip to content

Instantly share code, notes, and snippets.

@uncatcrea
Last active October 14, 2015 19:45
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 uncatcrea/3f50e35bab5fee013ec2 to your computer and use it in GitHub Desktop.
Save uncatcrea/3f50e35bab5fee013ec2 to your computer and use it in GitHub Desktop.
Shows how to use the 'template' filter to use a custom template for a given app screen
/**
Use a custom 'archive-my-category.html' template only for the
'my-category' category post list :
*/
App.filter( 'template', function( template, current_screen ) {
if ( TemplateTags.isCategory( 'my-category', current_screen ) ) {
template = 'archive-my-category'; //Don't need .html here.
}
return template;
} );
/**
Use a custom 'my-component-template.html' template only for the
'my-component-slug' component :
*/
App.filter( 'template', function( template, current_screen ) {
if ( current_screen.component_id == 'my-component-slug' ) {
template = 'my-component-template'; //Don't need .html here.
}
return template;
} );
/**
Use a custom 'single-my-post-type.html' template only for
posts that have the 'my-post-type' post type.
*/
App.filter( 'template', function( template, current_screen ) {
//Check the screen's post type with "TemplateTags.isPostType()".
//Note : as we have to pass the current_screen as 3rd argument, we pass an
//empty post_id=0 as 2nd argument ( because we don't have to check a specific
//post ID here, just the post type 'my-post-type' ):
if ( TemplateTags.isPostType( 'my-post-type', 0, current_screen ) ) {
template = 'single-my-post-type'; //Don't need .html here.
}
return template;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment