template-tags.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Spun
  8. */
  9. if ( ! function_exists( 'spun_content_nav' ) ):
  10. /**
  11. * Display navigation to next/previous pages when applicable
  12. *
  13. */
  14. function spun_content_nav( $nav_id ) {
  15. global $wp_query, $post;
  16. // Don't print empty markup on single pages if there's nowhere to navigate.
  17. if ( is_single() ) {
  18. $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
  19. $next = get_adjacent_post( false, '', false );
  20. if ( ! $next && ! $previous )
  21. return;
  22. }
  23. // Don't print empty markup in archives if there's only one page.
  24. if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
  25. return;
  26. $nav_class = 'site-navigation paging-navigation clear';
  27. if ( is_single() )
  28. $nav_class = 'site-navigation post-navigation clear';
  29. ?>
  30. <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo esc_attr( $nav_class ); ?>">
  31. <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'spun' ); ?></h1>
  32. <?php if ( is_single() ) : // navigation links for single posts ?>
  33. <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav screen-reader-text">' . _x( '&laquo; Previous Post', 'Previous post link', 'spun' ) . '</span>' ); ?>
  34. <?php next_post_link( '<div class="nav-next">%link</div>', '<span class="meta-nav screen-reader-text">' . _x( 'Next Post &raquo;', 'Next post link', 'spun' ) . '</span>' ); ?>
  35. <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
  36. <?php if ( get_next_posts_link() ) : ?>
  37. <div class="nav-previous"><span class="meta-nav"><?php next_posts_link( '<span class="screen-reader-text">' . _x( '&laquo;', 'Next posts link', 'spun' ) . '</span>' ); ?></span></div>
  38. <?php endif; ?>
  39. <?php if ( get_previous_posts_link() ) : ?>
  40. <div class="nav-next"><span class="meta-nav"><?php previous_posts_link( '<span class="screen-reader-text">' . _x( '&raquo;', 'Previous posts link', 'spun' ) . '</span>' ); ?></span></div>
  41. <?php endif; ?>
  42. <?php endif; ?>
  43. </nav><!-- #<?php echo $nav_id; ?> -->
  44. <?php
  45. }
  46. endif; // spun_content_nav
  47. if ( ! function_exists( 'spun_comment' ) ) :
  48. /**
  49. * Template for comments and pingbacks.
  50. *
  51. * Used as a callback by wp_list_comments() for displaying the comments.
  52. *
  53. */
  54. function spun_comment( $comment, $args, $depth ) {
  55. $GLOBALS['comment'] = $comment;
  56. switch ( $comment->comment_type ) :
  57. case 'pingback' :
  58. case 'trackback' :
  59. ?>
  60. <li class="post pingback">
  61. <p><?php _e( 'Pingback:', 'spun' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'spun' ), ' ' ); ?></p>
  62. <?php
  63. break;
  64. default :
  65. ?>
  66. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  67. <article id="comment-<?php comment_ID(); ?>" class="comment">
  68. <footer>
  69. <div class="reply">
  70. <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( '+', 'spun' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  71. </div><!-- .reply -->
  72. <div class="comment-author vcard">
  73. <?php echo get_avatar( $comment, 50 ); ?>
  74. <?php printf( __( '%s <span class="says">says:</span>', 'spun' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  75. </div><!-- .comment-author .vcard -->
  76. <?php if ( $comment->comment_approved == '0' ) : ?>
  77. <em><?php _e( 'Your comment is awaiting moderation.', 'spun' ); ?></em>
  78. <br />
  79. <?php endif; ?>
  80. <div class="comment-meta commentmetadata">
  81. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><time pubdate datetime="<?php comment_time( 'c' ); ?>">
  82. <?php
  83. /* translators: 1: date, 2: time */
  84. printf( __( '%1$s at %2$s', 'spun' ), get_comment_date(), get_comment_time() ); ?>
  85. </time></a>
  86. <?php edit_comment_link( __( 'Edit', 'spun' ), '| ' );
  87. ?>
  88. </div><!-- .comment-meta .commentmetadata -->
  89. </footer>
  90. <div class="comment-content"><?php comment_text(); ?></div>
  91. </article><!-- #comment-## -->
  92. <?php
  93. break;
  94. endswitch;
  95. }
  96. endif; // ends check for spun_comment()
  97. if ( ! function_exists( 'spun_posted_on' ) ) :
  98. /**
  99. * Prints HTML with meta information for the current post-date/time and author.
  100. *
  101. */
  102. function spun_posted_on() {
  103. printf( __( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="byline"> by <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'spun' ),
  104. esc_url( get_permalink() ),
  105. esc_attr( get_the_time() ),
  106. esc_attr( get_the_date( 'c' ) ),
  107. esc_html( get_the_date() ),
  108. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  109. esc_attr( sprintf( __( 'View all posts by %s', 'spun' ), get_the_author() ) ),
  110. esc_html( get_the_author() )
  111. );
  112. }
  113. endif;
  114. /**
  115. * Returns true if a blog has more than 1 category
  116. *
  117. */
  118. function spun_categorized_blog() {
  119. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  120. // Create an array of all the categories that are attached to posts
  121. $all_the_cool_cats = get_categories( array(
  122. 'hide_empty' => 1,
  123. ) );
  124. // Count the number of categories that are attached to the posts
  125. $all_the_cool_cats = count( $all_the_cool_cats );
  126. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  127. }
  128. if ( '1' != $all_the_cool_cats ) {
  129. // This blog has more than 1 category so spun_categorized_blog should return true
  130. return true;
  131. } else {
  132. // This blog has only 1 category so spun_categorized_blog should return false
  133. return false;
  134. }
  135. }
  136. /**
  137. * Flush out the transients used in spun_categorized_blog
  138. *
  139. */
  140. function spun_category_transient_flusher() {
  141. // Like, beat it. Dig?
  142. delete_transient( 'all_the_cool_cats' );
  143. }
  144. add_action( 'edit_category', 'spun_category_transient_flusher' );
  145. add_action( 'save_post', 'spun_category_transient_flusher' );
  146. /**
  147. * Get one image from a specified post in the following order:
  148. * Featured Image, first attached image, first image from the_content HTML
  149. * @param int $id, The post ID to check
  150. * @param string $size, The image size to return, defaults to 'post-thumbnail'
  151. * @return string $thumb, Thumbnail image with markup
  152. */
  153. function spun_get_image( $id, $size = 'home-post' ) {
  154. $thumb = '';
  155. if ( '' != get_the_post_thumbnail( $id ) ) {
  156. $thumb = get_the_post_thumbnail( $id, $size, array( 'title' => esc_attr( strip_tags( get_the_title() ) ) ) );
  157. }
  158. else {
  159. $args = array(
  160. 'post_type' => 'attachment',
  161. 'fields' => 'ids',
  162. 'numberposts' => 1,
  163. 'post_status' => null,
  164. 'post_mime_type' => 'image',
  165. 'post_parent' => $id,
  166. );
  167. $first_attachment = get_posts( $args );
  168. if ( $first_attachment ) {
  169. /* Get the first image attachment */
  170. foreach ( $first_attachment as $attachment ) {
  171. $thumb = wp_get_attachment_image( $attachment, $size, false, array( 'title' => esc_attr( strip_tags( get_the_title() ) ) ) );
  172. }
  173. }
  174. elseif ( class_exists( 'Jetpack_PostImages' ) ) {
  175. /* Get the first image directly from HTML content */
  176. $getimage = new Jetpack_PostImages();
  177. $image = $getimage->from_html( $id );
  178. if ( $image )
  179. $thumb = '<img src="' . $image[0]['src'] . '" title="' . esc_attr( strip_tags( get_the_title() ) ) . '" class="attachment-' . $size . ' wp-post-image" />';
  180. }
  181. }
  182. return $thumb;
  183. }