template-tags.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Custom Twenty Sixteen template tags
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features.
  6. *
  7. * @package WordPress
  8. * @subpackage Twenty_Sixteen
  9. * @since Twenty Sixteen 1.0
  10. */
  11. if ( ! function_exists( 'twentysixteen_entry_meta' ) ) :
  12. /**
  13. * Prints HTML with meta information for the categories, tags.
  14. *
  15. * Create your own twentysixteen_entry_meta() function to override in a child theme.
  16. *
  17. * @since Twenty Sixteen 1.0
  18. */
  19. function twentysixteen_entry_meta() {
  20. if ( 'post' === get_post_type() ) {
  21. $author_avatar_size = apply_filters( 'twentysixteen_author_avatar_size', 49 );
  22. printf( '<span class="byline"><span class="author vcard">%1$s<span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
  23. get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
  24. _x( 'Author', 'Used before post author name.', 'twentysixteen' ),
  25. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  26. get_the_author()
  27. );
  28. }
  29. if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
  30. twentysixteen_entry_date();
  31. }
  32. $format = get_post_format();
  33. if ( current_theme_supports( 'post-formats', $format ) ) {
  34. printf( '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
  35. sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'twentysixteen' ) ),
  36. esc_url( get_post_format_link( $format ) ),
  37. get_post_format_string( $format )
  38. );
  39. }
  40. if ( 'post' === get_post_type() ) {
  41. twentysixteen_entry_taxonomies();
  42. }
  43. if ( ! is_singular() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
  44. echo '<span class="comments-link">';
  45. comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentysixteen' ), get_the_title() ) );
  46. echo '</span>';
  47. }
  48. }
  49. endif;
  50. if ( ! function_exists( 'twentysixteen_entry_date' ) ) :
  51. /**
  52. * Prints HTML with date information for current post.
  53. *
  54. * Create your own twentysixteen_entry_date() function to override in a child theme.
  55. *
  56. * @since Twenty Sixteen 1.0
  57. */
  58. function twentysixteen_entry_date() {
  59. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  60. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  61. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  62. }
  63. $time_string = sprintf( $time_string,
  64. esc_attr( get_the_date( 'c' ) ),
  65. get_the_date(),
  66. esc_attr( get_the_modified_date( 'c' ) ),
  67. get_the_modified_date()
  68. );
  69. printf( '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
  70. _x( 'Posted on', 'Used before publish date.', 'twentysixteen' ),
  71. esc_url( get_permalink() ),
  72. $time_string
  73. );
  74. }
  75. endif;
  76. if ( ! function_exists( 'twentysixteen_entry_taxonomies' ) ) :
  77. /**
  78. * Prints HTML with category and tags for current post.
  79. *
  80. * Create your own twentysixteen_entry_taxonomies() function to override in a child theme.
  81. *
  82. * @since Twenty Sixteen 1.0
  83. */
  84. function twentysixteen_entry_taxonomies() {
  85. $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
  86. if ( $categories_list && twentysixteen_categorized_blog() ) {
  87. printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
  88. _x( 'Categories', 'Used before category names.', 'twentysixteen' ),
  89. $categories_list
  90. );
  91. }
  92. $tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
  93. if ( $tags_list ) {
  94. printf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
  95. _x( 'Tags', 'Used before tag names.', 'twentysixteen' ),
  96. $tags_list
  97. );
  98. }
  99. }
  100. endif;
  101. if ( ! function_exists( 'twentysixteen_post_thumbnail' ) ) :
  102. /**
  103. * Displays an optional post thumbnail.
  104. *
  105. * Wraps the post thumbnail in an anchor element on index views, or a div
  106. * element when on single views.
  107. *
  108. * Create your own twentysixteen_post_thumbnail() function to override in a child theme.
  109. *
  110. * @since Twenty Sixteen 1.0
  111. */
  112. function twentysixteen_post_thumbnail() {
  113. if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
  114. return;
  115. }
  116. if ( is_singular() ) :
  117. ?>
  118. <div class="post-thumbnail">
  119. <?php the_post_thumbnail(); ?>
  120. </div><!-- .post-thumbnail -->
  121. <?php else : ?>
  122. <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
  123. <?php the_post_thumbnail( 'post-thumbnail', array( 'alt' => the_title_attribute( 'echo=0' ) ) ); ?>
  124. </a>
  125. <?php endif; // End is_singular()
  126. }
  127. endif;
  128. if ( ! function_exists( 'twentysixteen_excerpt' ) ) :
  129. /**
  130. * Displays the optional excerpt.
  131. *
  132. * Wraps the excerpt in a div element.
  133. *
  134. * Create your own twentysixteen_excerpt() function to override in a child theme.
  135. *
  136. * @since Twenty Sixteen 1.0
  137. *
  138. * @param string $class Optional. Class string of the div element. Defaults to 'entry-summary'.
  139. */
  140. function twentysixteen_excerpt( $class = 'entry-summary' ) {
  141. $class = esc_attr( $class );
  142. if ( has_excerpt() || is_search() ) : ?>
  143. <div class="<?php echo $class; ?>">
  144. <?php the_excerpt(); ?>
  145. </div><!-- .<?php echo $class; ?> -->
  146. <?php endif;
  147. }
  148. endif;
  149. if ( ! function_exists( 'twentysixteen_excerpt_more' ) && ! is_admin() ) :
  150. /**
  151. * Replaces "[...]" (appended to automatically generated excerpts) with ... and
  152. * a 'Continue reading' link.
  153. *
  154. * Create your own twentysixteen_excerpt_more() function to override in a child theme.
  155. *
  156. * @since Twenty Sixteen 1.0
  157. *
  158. * @return string 'Continue reading' link prepended with an ellipsis.
  159. */
  160. function twentysixteen_excerpt_more() {
  161. $link = sprintf( '<a href="%1$s" class="more-link">%2$s</a>',
  162. esc_url( get_permalink( get_the_ID() ) ),
  163. /* translators: %s: Name of current post */
  164. sprintf( __( 'Continue reading %s', 'twentysixteen' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
  165. );
  166. return ' &hellip; ' . $link;
  167. }
  168. add_filter( 'excerpt_more', 'twentysixteen_excerpt_more' );
  169. endif;
  170. /**
  171. * Determines whether blog/site has more than one category.
  172. *
  173. * Create your own twentysixteen_categorized_blog() function to override in a child theme.
  174. *
  175. * @since Twenty Sixteen 1.0
  176. *
  177. * @return bool True if there is more than one category, false otherwise.
  178. */
  179. function twentysixteen_categorized_blog() {
  180. if ( false === ( $all_the_cool_cats = get_transient( 'twentysixteen_categories' ) ) ) {
  181. // Create an array of all the categories that are attached to posts.
  182. $all_the_cool_cats = get_categories( array(
  183. 'fields' => 'ids',
  184. // We only need to know if there is more than one category.
  185. 'number' => 2,
  186. ) );
  187. // Count the number of categories that are attached to the posts.
  188. $all_the_cool_cats = count( $all_the_cool_cats );
  189. set_transient( 'twentysixteen_categories', $all_the_cool_cats );
  190. }
  191. if ( $all_the_cool_cats > 1 ) {
  192. // This blog has more than 1 category so twentysixteen_categorized_blog should return true.
  193. return true;
  194. } else {
  195. // This blog has only 1 category so twentysixteen_categorized_blog should return false.
  196. return false;
  197. }
  198. }
  199. /**
  200. * Flushes out the transients used in twentysixteen_categorized_blog().
  201. *
  202. * @since Twenty Sixteen 1.0
  203. */
  204. function twentysixteen_category_transient_flusher() {
  205. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  206. return;
  207. }
  208. // Like, beat it. Dig?
  209. delete_transient( 'twentysixteen_categories' );
  210. }
  211. add_action( 'edit_category', 'twentysixteen_category_transient_flusher' );
  212. add_action( 'save_post', 'twentysixteen_category_transient_flusher' );