template.php 13 KB

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