extras.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Custom functions that act independently of the theme templates
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features
  6. *
  7. * @package Gazette
  8. */
  9. /**
  10. * Adds custom classes to the array of body classes.
  11. *
  12. * @param array $classes Classes for the body element.
  13. * @return array
  14. */
  15. function gazette_body_classes( $classes ) {
  16. // Adds a class of group-blog to blogs with more than 1 published author.
  17. if ( is_multi_author() ) {
  18. $classes[] = 'group-blog';
  19. }
  20. // Adds a class of has-header-image to blogs with a header image.
  21. if ( get_header_image() ) {
  22. $classes[] = 'has-header-image';
  23. }
  24. // Adds a class of no-sidebar-widget to blogs with no widgets in the sidebar area.
  25. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  26. $classes[] = 'no-sidebar-widget';
  27. }
  28. // Adds a class of no-footer-widget to blogs with no widgets in the footer area.
  29. if ( ! is_active_sidebar( 'sidebar-2' ) ) {
  30. $classes[] = 'no-footer-widget';
  31. }
  32. // Adds a class of unfixed-header to blogs with unfixed header theme option ticked.
  33. if ( 1 == get_theme_mod( 'gazette_unfixed_header' ) ) {
  34. $classes[] = 'unfixed-header';
  35. }
  36. return $classes;
  37. }
  38. add_filter( 'body_class', 'gazette_body_classes' );
  39. if ( version_compare( $GLOBALS['wp_version'], '4.1', '<' ) ) :
  40. /**
  41. * Filters wp_title to print a neat <title> tag based on what is being viewed.
  42. *
  43. * @param string $title Default title text for current view.
  44. * @param string $sep Optional separator.
  45. * @return string The filtered title.
  46. */
  47. function gazette_wp_title( $title, $sep ) {
  48. if ( is_feed() ) {
  49. return $title;
  50. }
  51. global $page, $paged;
  52. // Add the blog name
  53. $title .= get_bloginfo( 'name', 'display' );
  54. // Add the blog description for the home/front page.
  55. $site_description = get_bloginfo( 'description', 'display' );
  56. if ( $site_description && ( is_home() || is_front_page() ) ) {
  57. $title .= " $sep $site_description";
  58. }
  59. // Add a page number if necessary:
  60. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  61. $title .= " $sep " . sprintf( __( 'Page %s', 'gazette' ), max( $paged, $page ) );
  62. }
  63. return $title;
  64. }
  65. add_filter( 'wp_title', 'gazette_wp_title', 10, 2 );
  66. /**
  67. * Title shim for sites older than WordPress 4.1.
  68. *
  69. * @link https://make.wordpress.org/core/2014/10/29/title-tags-in-4-1/
  70. * @todo Remove this function when WordPress 4.3 is released.
  71. */
  72. function gazette_render_title() {
  73. ?>
  74. <title><?php wp_title( '|', true, 'right' ); ?></title>
  75. <?php
  76. }
  77. add_action( 'wp_head', 'gazette_render_title' );
  78. endif;
  79. if ( ! function_exists( 'gazette_excerpt_more' ) && ! is_admin() ) :
  80. /**
  81. * Replaces "[...]" (appended to automatically generated excerpts) with ...
  82. *
  83. * @since Gazette 1.0.4
  84. */
  85. function gazette_excerpt_more( $more ) {
  86. return ' &hellip;';
  87. }
  88. add_filter( 'excerpt_more', 'gazette_excerpt_more' );
  89. endif;
  90. if ( ! function_exists( 'gazette_continue_reading' ) && ! is_admin() ) :
  91. /**
  92. * Adds a "Continue reading" link to all instances of the_excerpt
  93. *
  94. * @since Gazette 1.0.5
  95. *
  96. * @return string The excerpt with a 'Continue reading' link appended.
  97. */
  98. function gazette_continue_reading( $the_excerpt ) {
  99. $the_excerpt = sprintf( '%1$s <a href="%2$s" class="more-link">%3$s</a>',
  100. $the_excerpt,
  101. esc_url( get_permalink( get_the_ID() ) ),
  102. /* translators: %s: Name of current post */
  103. sprintf( __( 'Continue reading %s', 'gazette' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
  104. );
  105. return $the_excerpt;
  106. }
  107. add_filter( 'the_excerpt', 'gazette_continue_reading', 9 );
  108. endif;
  109. /**
  110. * Custom lenght of the excerpt depending on the post type.
  111. */
  112. function gazette_excerpt_length( $length ) {
  113. if ( has_post_thumbnail() && ( has_post_format( 'image' ) || has_post_format( 'gallery' ) ) ) {
  114. return 26;
  115. } else if ( ( has_post_thumbnail() && ! has_post_format( 'aside' ) ) || has_post_format( 'link' ) || has_post_format( 'video' ) ) {
  116. return 40;
  117. } else if ( has_post_format( 'aside' ) ) {
  118. return 106;
  119. } else {
  120. return 100;
  121. }
  122. }
  123. add_filter( 'excerpt_length', 'gazette_excerpt_length', 999 );
  124. /**
  125. * Returns the URL from the post.
  126. *
  127. * @uses get_the_link() to get the URL in the post meta (if it exists) or
  128. * the first link found in the post content.
  129. *
  130. * Falls back to the post permalink if no URL is found in the post.
  131. *
  132. * @return string URL
  133. */
  134. function gazette_get_link_url() {
  135. $content = get_the_content();
  136. $has_url = get_url_in_content( $content );
  137. return ( $has_url && has_post_format( 'link' ) ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  138. }