Skip to content

Instantly share code, notes, and snippets.

@zanematthew
Created April 26, 2012 14:45
Show Gist options
  • Save zanematthew/2500075 to your computer and use it in GitHub Desktop.
Save zanematthew/2500075 to your computer and use it in GitHub Desktop.
Custom action for WordPress Template Redirect
<?php
/**
* Determine if given templates exists if they do loads them
* based on the type of page being displayed.
*
* @uses is_single()
*/
if ( ! function_exists( '_zm_template_redirect' ) ) :
function _zm_template_redirect( $params=array()){
extract( $params );
if ( is_single() ) {
if ( file_exists( $params['single'] ) ) {
load_template( $params['single'] );
exit();
}
} elseif ( is_post_type_archive('bmx-race-event') ) {
if ( file_exists( $params['archive'] ) ) {
load_template( $params['archive'] );
exit();
}
} elseif ( is_search() ) {
if ( file_exists( $params['search'] ) ) {
load_template( $params['search'] );
exit;
}
} elseif ( is_tax() ) {
if ( is_array( $params['taxonomy'] ) ) {
foreach( $params['taxonomy'] as $taxonomy ){
if ( file_exists( $taxonomy['template'] ) ) {
load_template( $taxonomy['template'] );
exit;
}
}
}
if ( file_exists( $params['taxonomy'] ) ) {
load_template( $params['taxonomy'] );
exit;
} else {
load_template( $params['taxonomy'] );
exit;
}
} else {
if ( file_exists( $params['default'] ) ) {
load_template( $params['default'] );
exit;
}
}
}
add_action( 'zm_template_redirect', '_zm_template_redirect', 1 );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment