template-tags.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Custom template tags for Twenty Fourteen
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Fourteen
  7. * @since Twenty Fourteen 1.0
  8. */
  9. if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
  10. /**
  11. * Display navigation to next/previous set of posts when applicable.
  12. *
  13. * @since Twenty Fourteen 1.0
  14. *
  15. * @return void
  16. */
  17. function twentyfourteen_paging_nav() {
  18. // Don't print empty markup if there's only one page.
  19. if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
  20. return;
  21. }
  22. $paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
  23. $pagenum_link = html_entity_decode( get_pagenum_link() );
  24. $query_args = array();
  25. $url_parts = explode( '?', $pagenum_link );
  26. if ( isset( $url_parts[1] ) ) {
  27. wp_parse_str( $url_parts[1], $query_args );
  28. }
  29. $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
  30. $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
  31. $format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
  32. $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
  33. // Set up paginated links.
  34. $links = paginate_links( array(
  35. 'base' => $pagenum_link,
  36. 'format' => $format,
  37. 'total' => $GLOBALS['wp_query']->max_num_pages,
  38. 'current' => $paged,
  39. 'mid_size' => 1,
  40. 'add_args' => array_map( 'urlencode', $query_args ),
  41. 'prev_text' => __( '&larr; Previous', 'twentyfourteen' ),
  42. 'next_text' => __( 'Next &rarr;', 'twentyfourteen' ),
  43. ) );
  44. if ( $links ) :
  45. ?>
  46. <nav class="navigation paging-navigation" role="navigation">
  47. <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
  48. <div class="pagination loop-pagination">
  49. <?php echo $links; ?>
  50. </div><!-- .pagination -->
  51. </nav><!-- .navigation -->
  52. <?php
  53. endif;
  54. }
  55. endif;
  56. if ( ! function_exists( 'twentyfourteen_post_nav' ) ) :
  57. /**
  58. * Display navigation to next/previous post when applicable.
  59. *
  60. * @since Twenty Fourteen 1.0
  61. *
  62. * @return void
  63. */
  64. function twentyfourteen_post_nav() {
  65. // Don't print empty markup if there's nowhere to navigate.
  66. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  67. $next = get_adjacent_post( false, '', false );
  68. if ( ! $next && ! $previous ) {
  69. return;
  70. }
  71. ?>
  72. <nav class="navigation post-navigation" role="navigation">
  73. <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfourteen' ); ?></h1>
  74. <div class="nav-links">
  75. <?php
  76. if ( is_attachment() ) :
  77. previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'twentyfourteen' ) );
  78. else :
  79. previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
  80. next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );
  81. endif;
  82. ?>
  83. </div><!-- .nav-links -->
  84. </nav><!-- .navigation -->
  85. <?php
  86. }
  87. endif;
  88. if ( ! function_exists( 'twentyfourteen_posted_on' ) ) :
  89. /**
  90. * Print HTML with meta information for the current post-date/time and author.
  91. *
  92. * @since Twenty Fourteen 1.0
  93. *
  94. * @return void
  95. */
  96. function twentyfourteen_posted_on() {
  97. if ( is_sticky() && is_home() && ! is_paged() ) {
  98. echo '<span class="featured-post">' . __( 'Sticky', 'twentyfourteen' ) . '</span>';
  99. }
  100. // Set up and print post meta information.
  101. printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>',
  102. esc_url( get_permalink() ),
  103. esc_attr( get_the_date( 'c' ) ),
  104. esc_html( get_the_date() ),
  105. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  106. get_the_author()
  107. );
  108. }
  109. endif;
  110. /**
  111. * Find out if blog has more than one category.
  112. *
  113. * @since Twenty Fourteen 1.0
  114. *
  115. * @return boolean true if blog has more than 1 category
  116. */
  117. function twentyfourteen_categorized_blog() {
  118. if ( false === ( $all_the_cool_cats = get_transient( 'twentyfourteen_category_count' ) ) ) {
  119. // Create an array of all the categories that are attached to posts
  120. $all_the_cool_cats = get_categories( array(
  121. 'hide_empty' => 1,
  122. ) );
  123. // Count the number of categories that are attached to the posts
  124. $all_the_cool_cats = count( $all_the_cool_cats );
  125. set_transient( 'twentyfourteen_category_count', $all_the_cool_cats );
  126. }
  127. if ( 1 !== (int) $all_the_cool_cats ) {
  128. // This blog has more than 1 category so twentyfourteen_categorized_blog should return true
  129. return true;
  130. } else {
  131. // This blog has only 1 category so twentyfourteen_categorized_blog should return false
  132. return false;
  133. }
  134. }
  135. /**
  136. * Flush out the transients used in twentyfourteen_categorized_blog.
  137. *
  138. * @since Twenty Fourteen 1.0
  139. *
  140. * @return void
  141. */
  142. function twentyfourteen_category_transient_flusher() {
  143. // Like, beat it. Dig?
  144. delete_transient( 'twentyfourteen_category_count' );
  145. }
  146. add_action( 'edit_category', 'twentyfourteen_category_transient_flusher' );
  147. add_action( 'save_post', 'twentyfourteen_category_transient_flusher' );
  148. /**
  149. * Display an optional post thumbnail.
  150. *
  151. * Wraps the post thumbnail in an anchor element on index
  152. * views, or a div element when on single views.
  153. *
  154. * @since Twenty Fourteen 1.0
  155. *
  156. * @return void
  157. */
  158. function twentyfourteen_post_thumbnail() {
  159. if ( post_password_required() || ! has_post_thumbnail() ) {
  160. return;
  161. }
  162. if ( is_singular() ) :
  163. ?>
  164. <div class="post-thumbnail">
  165. <?php
  166. if ( ( ! is_active_sidebar( 'sidebar-2' ) || is_page_template( 'page-templates/full-width.php' ) ) ) {
  167. the_post_thumbnail( 'twentyfourteen-full-width' );
  168. } else {
  169. the_post_thumbnail();
  170. }
  171. ?>
  172. </div>
  173. <?php else : ?>
  174. <a class="post-thumbnail" href="<?php the_permalink(); ?>">
  175. <?php
  176. if ( ( ! is_active_sidebar( 'sidebar-2' ) || is_page_template( 'page-templates/full-width.php' ) ) ) {
  177. the_post_thumbnail( 'twentyfourteen-full-width' );
  178. } else {
  179. the_post_thumbnail();
  180. }
  181. ?>
  182. </a>
  183. <?php endif; // End is_singular()
  184. }