template-tags.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Custom template tags for this theme.
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features
  6. *
  7. * @package bml
  8. */
  9. if ( ! function_exists( 'bml_content_nav' ) ) :
  10. /**
  11. * Display navigation to next/previous pages when applicable
  12. */
  13. function bml_content_nav( $nav_id ) {
  14. global $wp_query, $post;
  15. // Don't print empty markup on single pages if there's nowhere to navigate.
  16. if ( is_single() ) {
  17. $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
  18. $next = get_adjacent_post( false, '', false );
  19. if ( ! $next && ! $previous )
  20. return;
  21. }
  22. // Don't print empty markup in archives if there's only one page.
  23. if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
  24. return;
  25. $nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
  26. ?>
  27. <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
  28. <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'bml' ); ?></h1>
  29. <?php if ( is_single() ) : // navigation links for single posts ?>
  30. <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'bml' ) . '</span> %title' ); ?>
  31. <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'bml' ) . '</span>' ); ?>
  32. <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
  33. <?php if ( get_next_posts_link() ) : ?>
  34. <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'bml' ) ); ?></div>
  35. <?php endif; ?>
  36. <?php if ( get_previous_posts_link() ) : ?>
  37. <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'bml' ) ); ?></div>
  38. <?php endif; ?>
  39. <?php endif; ?>
  40. </nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
  41. <?php
  42. }
  43. endif; // bml_content_nav
  44. if ( ! function_exists( 'bml_comment' ) ) :
  45. /**
  46. * Template for comments and pingbacks.
  47. *
  48. * Used as a callback by wp_list_comments() for displaying the comments.
  49. */
  50. function bml_comment( $comment, $args, $depth ) {
  51. $GLOBALS['comment'] = $comment;
  52. if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
  53. <li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
  54. <div class="comment-body">
  55. <?php _e( 'Pingback:', 'bml' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'bml' ), '<span class="edit-link">', '</span>' ); ?>
  56. </div>
  57. <?php else : ?>
  58. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  59. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  60. <footer class="comment-meta">
  61. <div class="comment-author vcard">
  62. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  63. <?php printf( __( '%s <span class="says">says:</span>', 'bml' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  64. </div><!-- .comment-author -->
  65. <div class="comment-metadata">
  66. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
  67. <time datetime="<?php comment_time( 'c' ); ?>">
  68. <?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'bml' ), get_comment_date(), get_comment_time() ); ?>
  69. </time>
  70. </a>
  71. <?php edit_comment_link( __( 'Edit', 'bml' ), '<span class="edit-link">', '</span>' ); ?>
  72. </div><!-- .comment-metadata -->
  73. <?php if ( '0' == $comment->comment_approved ) : ?>
  74. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'bml' ); ?></p>
  75. <?php endif; ?>
  76. </footer><!-- .comment-meta -->
  77. <div class="comment-content">
  78. <?php comment_text(); ?>
  79. </div><!-- .comment-content -->
  80. <?php
  81. comment_reply_link( array_merge( $args, array(
  82. 'add_below' => 'div-comment',
  83. 'depth' => $depth,
  84. 'max_depth' => $args['max_depth'],
  85. 'before' => '<div class="reply">',
  86. 'after' => '</div>',
  87. ) ) );
  88. ?>
  89. </article><!-- .comment-body -->
  90. <?php
  91. endif;
  92. }
  93. endif; // ends check for bml_comment()
  94. if ( ! function_exists( 'bml_the_attached_image' ) ) :
  95. /**
  96. * Prints the attached image with a link to the next attached image.
  97. */
  98. function bml_the_attached_image() {
  99. $post = get_post();
  100. $attachment_size = apply_filters( 'bml_attachment_size', array( 1200, 1200 ) );
  101. $next_attachment_url = wp_get_attachment_url();
  102. /**
  103. * Grab the IDs of all the image attachments in a gallery so we can get the
  104. * URL of the next adjacent image in a gallery, or the first image (if
  105. * we're looking at the last image in a gallery), or, in a gallery of one,
  106. * just the link to that image file.
  107. */
  108. $attachment_ids = get_posts( array(
  109. 'post_parent' => $post->post_parent,
  110. 'fields' => 'ids',
  111. 'numberposts' => -1,
  112. 'post_status' => 'inherit',
  113. 'post_type' => 'attachment',
  114. 'post_mime_type' => 'image',
  115. 'order' => 'ASC',
  116. 'orderby' => 'menu_order ID'
  117. ) );
  118. // If there is more than 1 attachment in a gallery...
  119. if ( count( $attachment_ids ) > 1 ) {
  120. foreach ( $attachment_ids as $attachment_id ) {
  121. if ( $attachment_id == $post->ID ) {
  122. $next_id = current( $attachment_ids );
  123. break;
  124. }
  125. }
  126. // get the URL of the next image attachment...
  127. if ( $next_id )
  128. $next_attachment_url = get_attachment_link( $next_id );
  129. // or get the URL of the first image attachment.
  130. else
  131. $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
  132. }
  133. printf( '<a href="%1$s" rel="attachment">%2$s</a>',
  134. esc_url( $next_attachment_url ),
  135. wp_get_attachment_image( $post->ID, $attachment_size )
  136. );
  137. }
  138. endif;
  139. if ( ! function_exists( 'bml_posted_on' ) ) :
  140. /**
  141. * Prints HTML with meta information for the current post-date/time and author.
  142. */
  143. function bml_posted_on() {
  144. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
  145. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
  146. $time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
  147. $time_string = sprintf( $time_string,
  148. esc_attr( get_the_date( 'c' ) ),
  149. esc_html( get_the_date() ),
  150. esc_attr( get_the_modified_date( 'c' ) ),
  151. esc_html( get_the_modified_date() )
  152. );
  153. printf( __( '<span class="posted-on">Posted on %1$s</span><span class="byline"> by %2$s</span>', 'bml' ),
  154. sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>',
  155. esc_url( get_permalink() ),
  156. $time_string
  157. ),
  158. sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
  159. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  160. esc_html( get_the_author() )
  161. )
  162. );
  163. }
  164. endif;
  165. /**
  166. * Returns true if a blog has more than 1 category
  167. */
  168. function bml_categorized_blog() {
  169. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  170. // Create an array of all the categories that are attached to posts
  171. $all_the_cool_cats = get_categories( array(
  172. 'hide_empty' => 1,
  173. ) );
  174. // Count the number of categories that are attached to the posts
  175. $all_the_cool_cats = count( $all_the_cool_cats );
  176. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  177. }
  178. if ( '1' != $all_the_cool_cats ) {
  179. // This blog has more than 1 category so bml_categorized_blog should return true
  180. return true;
  181. } else {
  182. // This blog has only 1 category so bml_categorized_blog should return false
  183. return false;
  184. }
  185. }
  186. /**
  187. * Flush out the transients used in bml_categorized_blog
  188. */
  189. function bml_category_transient_flusher() {
  190. // Like, beat it. Dig?
  191. delete_transient( 'all_the_cool_cats' );
  192. }
  193. add_action( 'edit_category', 'bml_category_transient_flusher' );
  194. add_action( 'save_post', 'bml_category_transient_flusher' );