template.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /* <!-- SAVE -->
  3. if ( ! function_exists( 'dw_post_format') ) :
  4. function dw_post_format() {
  5. global $wp_query;
  6. $post_id = $wp_query->post->ID;
  7. if ( has_post_format( 'chat', $post_id )) {
  8. wp_enqueue_script( 'rainbow', get_template_directory_uri() . '/js/rainbow.min.js', '', '', true );
  9. }
  10. }
  11. endif;
  12. */
  13. if ( ! function_exists( 'page_bodyclass') ) :
  14. function page_bodyclass() {
  15. global $wp_query;
  16. $page = '';
  17. if (is_front_page() ) {
  18. $page = 'home';
  19. } elseif (is_page()) {
  20. $page = $wp_query->query_vars["pagename"];
  21. } elseif (is_single()) {
  22. $page = 'single';
  23. } elseif (is_archive()) {
  24. $page = 'archive';
  25. }
  26. if ($page)
  27. echo 'class="'. $page. '"';
  28. }
  29. endif;
  30. // ********* DEPRECATED wp_title in 4.4 ***********
  31. if ( ! function_exists( 'dw_page_title') ) :
  32. function dw_page_title() {
  33. global $page, $paged;
  34. wp_title( '|', true, 'right' );
  35. bloginfo( 'name' ); $site_description = get_bloginfo( 'description', 'display' );
  36. if ( $site_description && ( is_home() || is_front_page() ) )
  37. echo " | $site_description";
  38. if ( $paged >= 2 || $page >= 2 )
  39. echo ' | ' . sprintf( __( 'Page %s', 'dw' ), max( $paged, $page ) );
  40. }
  41. add_filter( 'document_title_separator', function () { return '|'; } );
  42. endif;
  43. /*========= Wait until WP 4.4 ============
  44. function filter_wp_title( $title, $sep ) {
  45. global $paged, $page;
  46. if ( is_feed() )
  47. return $title;
  48. $title .= get_bloginfo( 'name' );
  49. $site_description = get_bloginfo( 'description', 'display' );
  50. if ( $site_description && ( is_home() || is_front_page() ) )
  51. $title = "$title $sep $site_description";
  52. if ( $paged >= 2 || $page >= 2 )
  53. $title = "$title $sep " . sprintf( __( 'Page %s', 'dw' ), max( $paged, $page ) );
  54. return $title;
  55. }
  56. add_filter( 'wp_title', 'filter_wp_title', 10, 2 );
  57. add_filter( 'document_title_separator', function () { return '|'; } );
  58. */
  59. if ( ! function_exists( 'dw_meta_desc') ) :
  60. function dw_meta_desc() {
  61. global $post;
  62. $post = get_post( $post );
  63. setup_postdata( $post );
  64. if ( is_single() || is_page() ) {
  65. $meta_desc_value = get_post_meta( $post->ID, 'meta-desc', true );
  66. if ( $meta_desc_value !== '') {
  67. echo $meta_desc_value;
  68. }
  69. elseif ( $meta_desc_value == '') {
  70. $meta_excerpt = dw_good_excerpt(115);
  71. echo $meta_excerpt;
  72. }
  73. }
  74. }
  75. endif;
  76. if ( ! function_exists( 'dw_good_excerpt') ) :
  77. function dw_good_excerpt($length) {
  78. global $post;
  79. $text = $post->post_excerpt;
  80. if ( '' == $text ) {
  81. $text = get_the_content('');
  82. $text = apply_filters('the_content', $text);
  83. $text = str_replace(']]>', ']]>', $text);
  84. }
  85. $text = strip_shortcodes($text);
  86. $text = strip_tags($text);
  87. $text = substr($text,0,$length);
  88. if ( strlen($text) < 15 ) {
  89. $text = the_title('', ' - ', false);
  90. }
  91. $allowed_end = array('.', '!', '?', '...');
  92. $excerpt = reverse_strrchr($text, '.', 2);
  93. if( $excerpt ) {
  94. echo apply_filters('get_the_excerpt',$excerpt);
  95. } else {
  96. echo apply_filters('get_the_excerpt',$text);
  97. }
  98. }
  99. function reverse_strrchr($haystack, $needle, $trail) {
  100. return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
  101. }
  102. endif;
  103. if ( ! function_exists( 'dw_excerpt') ) :
  104. function dw_excerpt($count){
  105. $permalink = get_permalink($post->ID);
  106. $excerpt = get_the_content();
  107. $excerpt = strip_tags($excerpt);
  108. $excerpt = substr($excerpt, 0, $count);
  109. $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  110. return $excerpt;
  111. }
  112. endif;
  113. if ( ! function_exists( 'dw_paging_nav' ) ) :
  114. function dw_paging_nav() {
  115. if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
  116. return;
  117. }
  118. $paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
  119. $pagenum_link = html_entity_decode( get_pagenum_link() );
  120. $query_args = array();
  121. $url_parts = explode( '?', $pagenum_link );
  122. if ( isset( $url_parts[1] ) ) {
  123. wp_parse_str( $url_parts[1], $query_args );
  124. }
  125. $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
  126. $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
  127. $format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
  128. $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
  129. $links = paginate_links( array(
  130. 'base' => $pagenum_link,
  131. 'format' => $format,
  132. 'total' => $GLOBALS['wp_query']->max_num_pages,
  133. 'current' => $paged,
  134. 'mid_size' => 1,
  135. 'add_args' => array_map( 'urlencode', $query_args ),
  136. 'prev_text' => __( '&larr; &nbsp;', 'dw' ),
  137. 'next_text' => __( '&nbsp; &rarr;', 'dw' ),
  138. ) );
  139. if ( $links ) :
  140. ?>
  141. <nav class="navigation pagination paging-navigation" role="navigation">
  142. <div class="pagination loop-pagination">
  143. <?php echo $links; ?>
  144. </div>
  145. </nav>
  146. <?php
  147. endif;
  148. }
  149. endif;
  150. if ( ! function_exists( 'dw_posted_at' ) ) :
  151. function dw_posted_at() {
  152. $categories_list = get_the_category_list( __( ', ', 'dw' ) );
  153. if ( in_category( 'fb' )) {
  154. echo '<div class="categories-fb"><a href="http://facebook.com/windhamdavid" target="_blank">posted on <span class="fb">&nbsp;</span></a></div>';
  155. } elseif ( in_category( array( 'twitter', 'tumblr' ) )) {
  156. } else {
  157. echo '<span class="categories-links">' . $categories_list . '</span>';
  158. }
  159. }
  160. endif;
  161. if ( ! function_exists( 'dw_posted_on' ) ) :
  162. function dw_posted_on() {
  163. return sprintf( __( '<span %1$s></span> %2$s by %3$s', 'daw' ),
  164. 'class="meta-prep meta-prep-author"',
  165. sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a> <span class="meta-sep">',
  166. get_permalink(),
  167. esc_attr( get_the_time() ),
  168. get_the_date()
  169. ),
  170. sprintf( '</span> <span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
  171. get_author_posts_url( get_the_author_meta( 'ID' ) ),
  172. sprintf( esc_attr__( 'View all posts by %s', 'daw' ), get_the_author() ),
  173. get_the_author()
  174. )
  175. );
  176. }
  177. endif;
  178. if ( ! function_exists( 'dw_posted_in' ) ) :
  179. function dw_posted_in() {
  180. $tag_list = get_the_tag_list( '', ', ', '' );
  181. if ( $tag_list ) {
  182. $utility_text = __( '%1$s and tagged %2$s. <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'dw' );
  183. } else {
  184. $utility_text = __( '%1$s. <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'dw' );
  185. }
  186. return sprintf(
  187. $utility_text,
  188. get_the_category_list( ', ' ),
  189. $tag_list,
  190. get_permalink(),
  191. the_title_attribute( 'echo=0' ),
  192. get_post_comments_feed_link()
  193. );
  194. }
  195. endif;
  196. if ( ! function_exists( 'dw_get_next_attachment_url' ) ) :
  197. function dw_get_next_attachment_url() {
  198. global $post;
  199. $post = get_post( $post );
  200. $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
  201. foreach ( $attachments as $k => $attachment ) {
  202. if ( $attachment->ID == $post->ID )
  203. break;
  204. }
  205. $k++;
  206. if ( isset( $attachments[ $k ] ) )
  207. return get_attachment_link( $attachments[ $k ]->ID );
  208. else
  209. return get_permalink( $post->post_parent );
  210. }
  211. endif;
  212. if ( ! function_exists( 'dw_post_nav' ) ) :
  213. function dw_post_nav() {
  214. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  215. $next = get_adjacent_post( false, '', false );
  216. if ( ! $next && ! $previous ) {
  217. return;
  218. }
  219. ?>
  220. <nav class="navigation post-navigation" role="navigation">
  221. <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'dw' ); ?></h1>
  222. <div class="nav-links">
  223. <?php
  224. if ( is_attachment() ) :
  225. previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'dw' ) );
  226. else :
  227. previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'dw' ) );
  228. next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'dw' ) );
  229. endif;
  230. ?>
  231. </div>
  232. </nav>
  233. <?php
  234. }
  235. endif;
  236. if ( ! function_exists( 'dw_cat_list' ) ) :
  237. function dw_cat_list() {
  238. return daw_term_list( 'category', ', ', __( 'Posted in %s', 'dw' ), __( 'Also posted in %s', 'daw' ) );
  239. }
  240. endif;
  241. if ( ! function_exists( 'dw_tag_list' ) ) :
  242. function dw_tag_list() {
  243. return daw_term_list( 'post_tag', ', ', __( 'Tagged %s', 'dw' ), __( 'Also tagged %s', 'daw' ) );
  244. }
  245. endif;
  246. if ( ! function_exists( 'dw_term_list' ) ) :
  247. function daw_term_list( $taxonomy, $glue = ', ', $text = '', $also_text = '' ) {
  248. global $wp_query, $post;
  249. $current_term = $wp_query->get_queried_object();
  250. $terms = wp_get_object_terms( $post->ID, $taxonomy );
  251. if ( isset( $current_term->taxonomy ) && $taxonomy == $current_term->taxonomy ) {
  252. foreach ( (array) $terms as $key => $term ) {
  253. if ( $term->term_id == $current_term->term_id ) {
  254. unset( $terms[$key] );
  255. break;
  256. }
  257. }
  258. $text = $also_text;
  259. }
  260. $tlist = array();
  261. $rel = 'category' == $taxonomy ? 'rel="category"' : 'rel="tag"';
  262. foreach ( (array) $terms as $term ) {
  263. $tlist[] = '<a href="' . get_term_link( $term, $taxonomy ) . '" title="' . esc_attr( sprintf( __( 'View all posts in %s', 'daw' ), $term->name ) ) . '" ' . $rel . '>' . $term->name . '</a>';
  264. }
  265. if ( ! empty( $tlist ) )
  266. return sprintf( $text, join( $glue, $tlist ) );
  267. return '';
  268. }
  269. endif;
  270. if ( ! function_exists( 'dw_comment' ) ) :
  271. function dw_comment( $comment, $args, $depth ) {
  272. $GLOBALS ['comment'] = $comment; ?>
  273. <?php if ( '' == $comment->comment_type ) : ?>
  274. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  275. <div id="comment-<?php comment_ID(); ?>">
  276. <div class="comment-author vcard">
  277. <?php echo get_avatar( $comment, 64 ); ?>
  278. <?php printf( __( '<cite class="fn">%s</cite><span class="says">:</span>', 'daw' ), get_comment_author_link() ); ?>
  279. </div>
  280. <?php if ( $comment->comment_approved == '0' ) : ?>
  281. <em><?php _e( 'Your comment is awaiting moderation.', 'daw' ); ?></em>
  282. <br />
  283. <?php endif; ?>
  284. <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php printf( __( '%1$s at %2$s', 'daw' ), get_comment_date(), get_comment_time() ); ?></a></div>
  285. <div class="comment-body"><?php comment_text(); ?></div>
  286. <div class="reply">
  287. <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  288. <?php edit_comment_link( __( '(Edit)', 'daw' ),' ','' ); ?>
  289. </div>
  290. </div>
  291. <?php else : ?>
  292. <li class="post pingback">
  293. <p><?php _e( 'Pingback: ', 'daw' ); ?><?php comment_author_link(); ?><?php edit_comment_link ( __('edit', 'daw'), '&nbsp;&nbsp;', '' ); ?></p>
  294. <?php endif;
  295. }
  296. endif;
  297. add_filter( 'comment_form_default_fields', 'dw_comment_form_fields' );
  298. function dw_comment_form_fields( $fields ) {
  299. $commenter = wp_get_current_commenter();
  300. $req = get_option( 'require_name_email' );
  301. $aria_req = ( $req ? " aria-required='true'" : '' );
  302. $html5 = current_theme_supports( 'html5', 'comment-form' ) ? 1 : 0;
  303. $fields = array(
  304. 'author' => '<div class="form-group comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  305. '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div><input class="form-control" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /><span class="add-on"><i class="icon-user"></i></span></div></div>',
  306. 'email' => '<div class="form-group comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  307. '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div><input class="form-control" id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></div></div>',
  308. 'url' => '<div class="form-group comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
  309. '<div class="input-group"><div class="input-group-addon"><span class="glyphicon glyphicon-link" aria-hidden="true"></span></div><input class="form-control" id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></div></div>',
  310. );
  311. return $fields;
  312. }
  313. add_filter( 'comment_form_defaults', 'dw_comment_form' );
  314. function dw_comment_form( $args ) {
  315. $args['comment_field'] = '<div class="form-group comment-form-comment">
  316. <label for="comment">' . _x( 'Comment', 'noun' ) . '</label>
  317. <textarea class="form-control" id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>
  318. </div>';
  319. return $args;
  320. }
  321. add_action('comment_form', 'dw_comment_button' );
  322. function dw_comment_button() {
  323. echo '<button class="btn btn-default" type="submit">' . __( 'Submit' ) . '</button>';
  324. }