extras.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Publication
  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 publication_body_classes( $classes ) {
  16. // Adds a class of has-hero to the body if first post on the blog or single has a featured image.
  17. if ( ( is_home() && ! is_paged() && has_post_thumbnail() ) || ( is_singular() && has_post_thumbnail() ) ) {
  18. $classes[] = 'has-hero';
  19. }
  20. // Adds a class of no-sidebar to the body when the sidebar is missing.
  21. if ( ! is_active_sidebar( 'sidebar-1' ) && ! is_active_sidebar( 'sidebar-2' ) ) {
  22. $classes[] = 'no-sidebar';
  23. }
  24. // Adds a class of no-menu to the body when the menus are missing.
  25. if ( ! has_nav_menu( 'primary' ) && ! has_nav_menu( 'social' ) ) {
  26. $classes[] = 'no-menu';
  27. }
  28. return $classes;
  29. }
  30. add_filter( 'body_class', 'publication_body_classes' );
  31. /**
  32. * Adds custom classes to the array of post classes.
  33. *
  34. * @param array $classes Classes for the post element.
  35. * @return array
  36. */
  37. function publication_post_classes( $classes ) {
  38. global $wp_query;
  39. // Adds a class of hero to the 1st post on the 1st page if it has a featured image.
  40. if ( is_home() && ! is_paged() && 0 === $wp_query->current_post && has_post_thumbnail() ) {
  41. $classes[] = 'hero';
  42. }
  43. return $classes;
  44. }
  45. add_filter( 'post_class', 'publication_post_classes' );
  46. if ( ! function_exists( 'publication_excerpt_more' ) && ! is_admin() ) :
  47. /**
  48. * Replaces "[...]" (appended to automatically generated excerpts) with ...
  49. *
  50. * @since Publication 1.0
  51. */
  52. function publication_excerpt_more( $more ) {
  53. return ' &hellip;';
  54. }
  55. add_filter( 'excerpt_more', 'publication_excerpt_more' );
  56. endif;
  57. if ( ! function_exists( 'publication_continue_reading' ) && ! is_admin() ) :
  58. /**
  59. * Adds a "Continue reading" link to all instances of the_excerpt
  60. *
  61. * @since Publication 1.0.2
  62. *
  63. * @return string The excerpt with a 'Continue reading' link appended.
  64. */
  65. function publication_continue_reading( $the_excerpt ) {
  66. $the_excerpt = sprintf( '%1$s <a href="%2$s" class="more-link">%3$s</a>',
  67. $the_excerpt,
  68. esc_url( get_permalink( get_the_ID() ) ),
  69. /* translators: %s: Name of current post */
  70. sprintf( __( 'Continue reading %s', 'publication' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
  71. );
  72. return $the_excerpt;
  73. }
  74. add_filter( 'the_excerpt', 'publication_continue_reading', 9 );
  75. endif;
  76. /**
  77. * Custom lenght of the excerpt depending on the post.
  78. */
  79. function publication_excerpt_length( $length ) {
  80. return 20;
  81. }
  82. add_filter( 'excerpt_length', 'publication_excerpt_length', 999 );
  83. /**
  84. * Add featured image as background image to hero.
  85. *
  86. * @see wp_add_inline_style()
  87. */
  88. function publication_hero_background() {
  89. global $wp_query;
  90. global $post;
  91. if ( ( ! is_single() && ! has_post_thumbnail() ) || ( ! is_home() && ! is_paged() && 0 !== $wp_query->current_post && ! has_post_thumbnail() ) ) {
  92. return;
  93. }
  94. $featuredImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'publication-hero' );
  95. $css = '.hero { background-image: url(' . esc_url( $featuredImage[0] ) . '); }';
  96. wp_add_inline_style( 'publication-style', $css );
  97. }
  98. add_action( 'wp_enqueue_scripts', 'publication_hero_background' );
  99. /**
  100. * Add featured image as background image to post navigation elements.
  101. *
  102. * @see wp_add_inline_style()
  103. */
  104. function publication_post_nav_background() {
  105. if ( ! is_single() ) {
  106. return;
  107. }
  108. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  109. $next = get_adjacent_post( false, '', false );
  110. $css = '';
  111. if ( is_attachment() && 'attachment' == $previous->post_type ) {
  112. return;
  113. }
  114. if ( $previous && has_post_thumbnail( $previous->ID ) ) {
  115. $prevThumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'publication-navigation' );
  116. $css .= '
  117. .post-navigation .nav-previous { background-image: url(' . esc_url( $prevThumb[0] ) . '); }
  118. .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
  119. .post-navigation .nav-previous .meta-nav { box-shadow: inset 0 -0.5em 0 #222; }
  120. .post-navigation .nav-previous a { background-color: rgba(0, 0, 0, 0.25); border: 0; text-shadow: 0 0 0.125em rgba(0, 0, 0, 0.5); }
  121. .post-navigation .nav-previous a:active, .post-navigation .nav-previous a:focus, .post-navigation .nav-previous a:hover { background-color: rgba(0, 0, 0, 0.5); }
  122. .post-navigation .nav-previous a:active .meta-nav, .post-navigation .nav-previous a:focus .meta-nav, .post-navigation .nav-previous a:hover .meta-nav { background: #222; }
  123. .post-navigation .nav-previous a:focus .post-title { color: #fff; }
  124. ';
  125. }
  126. if ( $next && has_post_thumbnail( $next->ID ) ) {
  127. $nextThumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'publication-navigation' );
  128. $css .= '
  129. .post-navigation .nav-next { background-image: url(' . esc_url( $nextThumb[0] ) . '); }
  130. .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
  131. .post-navigation .nav-next .meta-nav { box-shadow: inset 0 -0.5em 0 #222; }
  132. .post-navigation .nav-next a { background-color: rgba(0, 0, 0, 0.25); border: 0; text-shadow: 0 0 0.125em rgba(0, 0, 0, 0.5); }
  133. .post-navigation .nav-next a:active, .post-navigation .nav-next a:focus, .post-navigation .nav-next a:hover { background-color: rgba(0, 0, 0, 0.5); }
  134. .post-navigation .nav-next a:active .meta-nav, .post-navigation .nav-next a:focus .meta-nav, .post-navigation .nav-next a:hover .meta-nav { background: #222; }
  135. .post-navigation .nav-next a:focus .post-title { color: #fff; }
  136. ';
  137. }
  138. wp_add_inline_style( 'publication-style', $css );
  139. }
  140. add_action( 'wp_enqueue_scripts', 'publication_post_nav_background' );