PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/template-hierarchy.php

https://bitbucket.org/lgorence/quickpress
PHP | 211 lines | 72 code | 43 blank | 96 comment | 7 complexity | 7db5e8f1ad0314159db1806eb5ef7697 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The framework has its own template hierarchy that can be used instead of the default WordPress
  4. * template hierarchy. It is not much different than the default. It was built to extend the default by
  5. * making it smarter and more flexible. The goal is to give theme developers and end users an
  6. * easy-to-override system that doesn't involve massive amounts of conditional tags within files.
  7. *
  8. * @package HybridCore
  9. * @subpackage Functions
  10. * @author Justin Tadlock <justin@justintadlock.com>
  11. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  12. * @link http://themehybrid.com/hybrid-core
  13. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  14. */
  15. /* Filter the date template. */
  16. add_filter( 'date_template', 'hybrid_date_template' );
  17. /* Filter the author/user template. */
  18. add_filter( 'author_template', 'hybrid_user_template' );
  19. /* Filter the tag and category (taxonomy) templates. */
  20. add_filter( 'tag_template', 'hybrid_taxonomy_template' );
  21. add_filter( 'category_template', 'hybrid_taxonomy_template' );
  22. add_filter( 'taxonomy_template', 'hybrid_taxonomy_template' );
  23. /* Filter the single, page, and attachment (singular) templates. */
  24. add_filter( 'single_template', 'hybrid_singular_template' );
  25. add_filter( 'page_template', 'hybrid_singular_template' );
  26. add_filter( 'attachment_template', 'hybrid_singular_template' );
  27. /**
  28. * Overrides WP's default template for date-based archives. Better abstraction of templates than
  29. * is_date() allows by checking for the year, month, week, day, hour, and minute.
  30. *
  31. * @since 0.6.0
  32. * @access private
  33. * @uses locate_template() Checks for template in child and parent theme.
  34. * @param string $template
  35. * @return string $template Full path to file.
  36. */
  37. function hybrid_date_template( $template ) {
  38. $templates = array();
  39. /* If viewing a time-based archive. */
  40. if ( is_time() ) {
  41. /* If viewing a minutely archive. */
  42. if ( get_query_var( 'minute' ) )
  43. $templates[] = 'minute.php';
  44. /* If viewing an hourly archive. */
  45. elseif ( get_query_var( 'hour' ) )
  46. $templates[] = 'hour.php';
  47. /* Catchall for any time-based archive. */
  48. $templates[] = 'time.php';
  49. }
  50. /* If viewing a daily archive. */
  51. elseif ( is_day() )
  52. $templates[] = 'day.php';
  53. /* If viewing a weekly archive. */
  54. elseif ( get_query_var( 'w' ) )
  55. $templates[] = 'week.php';
  56. /* If viewing a monthly archive. */
  57. elseif ( is_month() )
  58. $templates[] = 'month.php';
  59. /* If viewing a yearly archive. */
  60. elseif ( is_year() )
  61. $templates[] = 'year.php';
  62. /* Catchall template for date-based archives. */
  63. $templates[] = 'date.php';
  64. /* Fall back to the basic archive template. */
  65. $templates[] = 'archive.php';
  66. /* Return the found template. */
  67. return locate_template( $templates );
  68. }
  69. /**
  70. * Overrides WP's default template for author-based archives. Better abstraction of templates than
  71. * is_author() allows by allowing themes to specify templates for a specific author. The hierarchy is
  72. * user-$nicename.php, $user-role-$role.php, user.php, author.php, archive.php.
  73. *
  74. * @since 0.7.0
  75. * @access private
  76. * @uses locate_template() Checks for template in child and parent theme.
  77. * @param string $template
  78. * @return string Full path to file.
  79. */
  80. function hybrid_user_template( $template ) {
  81. $templates = array();
  82. /* Get the user nicename. */
  83. $name = get_the_author_meta( 'user_nicename', get_query_var( 'author' ) );
  84. /* Get the user object. */
  85. $user = new WP_User( absint( get_query_var( 'author' ) ) );
  86. /* Add the user nicename template. */
  87. $templates[] = "user-{$name}.php";
  88. /* Add role-based templates for the user. */
  89. if ( is_array( $user->roles ) ) {
  90. foreach ( $user->roles as $role )
  91. $templates[] = "user-role-{$role}.php";
  92. }
  93. /* Add a basic user template. */
  94. $templates[] = 'user.php';
  95. /* Add backwards compatibility with the WordPress author template. */
  96. $templates[] = 'author.php';
  97. /* Fall back to the basic archive template. */
  98. $templates[] = 'archive.php';
  99. /* Return the found template. */
  100. return locate_template( $templates );
  101. }
  102. /**
  103. * Overrides WP's default template for category- and tag-based archives. This allows better
  104. * organization of taxonomy template files by making categories and post tags work the same way as
  105. * other taxonomies. The hierarchy is taxonomy-$taxonomy-$term.php, taxonomy-$taxonomy.php,
  106. * taxonomy.php, archive.php.
  107. *
  108. * @since 0.7.0
  109. * @access private
  110. * @uses locate_template() Checks for template in child and parent theme.
  111. * @param string $template
  112. * @return string Full path to file.
  113. */
  114. function hybrid_taxonomy_template( $template ) {
  115. /* Get the queried term object. */
  116. $term = get_queried_object();
  117. /* Remove 'post-format' from the slug. */
  118. $slug = ( ( 'post_format' == $term->taxonomy ) ? str_replace( 'post-format-', '', $term->slug ) : $term->slug );
  119. /* Return the available templates. */
  120. return locate_template( array( "taxonomy-{$term->taxonomy}-{$slug}.php", "taxonomy-{$term->taxonomy}.php", 'taxonomy.php', 'archive.php' ) );
  121. }
  122. /**
  123. * Overrides the default single (singular post) template. Post templates can be loaded using a custom
  124. * post template, by slug, or by ID.
  125. *
  126. * Attachment templates are handled slightly differently. Rather than look for the slug
  127. * or ID, templates can be loaded by attachment-$mime[0]_$mime[1].php,
  128. * attachment-$mime[1].php, or attachment-$mime[0].php.
  129. *
  130. * @since 0.7.0
  131. * @access private
  132. * @param string $template The default WordPress post template.
  133. * @return string $template The theme post template after all templates have been checked for.
  134. */
  135. function hybrid_singular_template( $template ) {
  136. $templates = array();
  137. /* Get the queried post. */
  138. $post = get_queried_object();
  139. /* Check for a custom post template by custom field key '_wp_post_template'. */
  140. $custom = get_post_meta( get_queried_object_id(), "_wp_{$post->post_type}_template", true );
  141. if ( $custom )
  142. $templates[] = $custom;
  143. /* If viewing an attachment page, handle the files by mime type. */
  144. if ( is_attachment() ) {
  145. /* Split the mime_type into two distinct parts. */
  146. $type = explode( '/', get_post_mime_type() );
  147. $templates[] = "attachment-{$type[0]}_{$type[1]}.php";
  148. $templates[] = "attachment-{$type[1]}.php";
  149. $templates[] = "attachment-{$type[0]}.php";
  150. }
  151. /* If viewing any other type of singular page. */
  152. else {
  153. /* Add a post name (slug) template. */
  154. $templates[] = "{$post->post_type}-{$post->post_name}.php";
  155. /* Add a post ID template. */
  156. $templates[] = "{$post->post_type}-{$post->ID}.php";
  157. }
  158. /* Add a template based off the post type name. */
  159. $templates[] = "{$post->post_type}.php";
  160. /* Allow for WP standard 'single' templates for compatibility. */
  161. $templates[] = "single-{$post->post_type}.php";
  162. $templates[] = 'single.php';
  163. /* Add a general template of singular.php. */
  164. $templates[] = "singular.php";
  165. /* Return the found template. */
  166. return locate_template( $templates );
  167. }
  168. ?>