Last active
September 27, 2015 09:47
-
-
Save wpscholar/1250124 to your computer and use it in GitHub Desktop.
WordPress Contextual Template Loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WordPress contextual template loading | |
*/ | |
function mpress_load_template( $name ){ | |
if( is_front_page() ){ | |
if( is_home() ){ | |
return locate_template( array( | |
$name.'-home.php', | |
$name.'.php' | |
), true ); | |
} else{ | |
return locate_template( array( | |
$name.'-front.php', | |
$name.'-page.php', | |
$name.'.php' | |
), true ); | |
} | |
} elseif( is_home() ){ | |
return locate_template( array( | |
$name.'-home.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_singular() ){ | |
if( is_page() ){ | |
$slug = get_queried_object()->post_name; | |
$id = get_queried_object()->ID; | |
return locate_template( array( | |
$name.'-page-'.$slug.'.php', | |
$name.'-page-'.$id.'.php', | |
$name.'-page.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_single() ){ | |
$post_type = get_post_type(); | |
return locate_template( array( | |
$name.'-single-'.$post_type.'-'.$slug.'.php', | |
$name.'-single-'.$post_type.'-'.$id.'.php', | |
$name.'-single-'.$post_type.'.php', | |
$name.'-single.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_attachment() ){ | |
return locate_template( array( | |
$name.'-attachment.php', | |
$name.'-single.php', | |
$name.'.php' | |
), true ); | |
} else{ | |
return locate_template( array( | |
$name.'-single.php', | |
$name.'.php' | |
), true ); | |
} | |
} elseif( is_archive() ){ | |
if( is_category() ){ | |
$slug = get_queried_object()->slug; | |
$id = get_queried_object()->term_id; | |
return locate_template( array( | |
$name.'-category-'.$slug.'.php', | |
$name.'-category-'.$id.'.php', | |
$name.'-category.php', | |
$name.'-taxonomy.php', | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_tag() ){ | |
$slug = get_queried_object()->slug; | |
$id = get_queried_object()->term_id; | |
return locate_template( array( | |
$name.'-tag-'.$slug.'.php', | |
$name.'-tag-'.$id.'.php', | |
$name.'-tag.php', | |
$name.'-taxonomy.php', | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_tax() ){ | |
$taxonomy = get_queried_object()->taxonomy; | |
$term = get_queried_object()->slug; | |
return locate_template( array( | |
$name.'-taxonomy-'.$taxonomy.'-'.$term.'.php', | |
$name.'-taxonomy-'.$taxonomy.'.php', | |
$name.'-taxonomy.php', | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_author() ){ | |
$nicename = get_queried_object()->user_nicename; | |
$id = get_queried_object()->ID; | |
return locate_template( array( | |
$name.'-author-'.$nicename.'.php', | |
$name.'-author-'.$id.'.php', | |
$name.'-author.php', | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_date() ){ | |
return locate_template( array( | |
$name.'-date.php', | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} else { | |
return locate_template( array( | |
$name.'-archive.php', | |
$name.'.php' | |
), true ); | |
} | |
} elseif( is_search() ){ | |
return locate_template( array( | |
$name.'-search.php', | |
$name.'.php' | |
), true ); | |
} elseif( is_404() ){ | |
return locate_template( array( | |
$name.'-404.php', | |
$name.'.php' | |
), true ); | |
} else{ | |
return locate_template( array( | |
$name.'.php' | |
), true ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment