暫無描述

template-functions.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Functions which enhance the theme by hooking into WordPress
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Nineteen
  7. * @since Twenty Nineteen 1.0
  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 twentynineteen_body_classes( $classes ) {
  16. if ( is_singular() ) {
  17. // Adds `singular` to singular pages.
  18. $classes[] = 'singular';
  19. } else {
  20. // Adds `hfeed` to non-singular pages.
  21. $classes[] = 'hfeed';
  22. }
  23. // Adds a class if image filters are enabled.
  24. if ( twentynineteen_image_filters_enabled() ) {
  25. $classes[] = 'image-filters-enabled';
  26. }
  27. return $classes;
  28. }
  29. add_filter( 'body_class', 'twentynineteen_body_classes' );
  30. /**
  31. * Adds custom class to the array of posts classes.
  32. */
  33. function twentynineteen_post_classes( $classes, $class, $post_id ) {
  34. $classes[] = 'entry';
  35. return $classes;
  36. }
  37. add_filter( 'post_class', 'twentynineteen_post_classes', 10, 3 );
  38. /**
  39. * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  40. */
  41. function twentynineteen_pingback_header() {
  42. if ( is_singular() && pings_open() ) {
  43. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  44. }
  45. }
  46. add_action( 'wp_head', 'twentynineteen_pingback_header' );
  47. /**
  48. * Changes comment form default fields.
  49. */
  50. function twentynineteen_comment_form_defaults( $defaults ) {
  51. $comment_field = $defaults['comment_field'];
  52. // Adjust height of comment form.
  53. $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  54. return $defaults;
  55. }
  56. add_filter( 'comment_form_defaults', 'twentynineteen_comment_form_defaults' );
  57. /**
  58. * Filters the default archive titles.
  59. */
  60. function twentynineteen_get_the_archive_title() {
  61. if ( is_category() ) {
  62. $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  63. } elseif ( is_tag() ) {
  64. $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  65. } elseif ( is_author() ) {
  66. $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  67. } elseif ( is_year() ) {
  68. $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
  69. } elseif ( is_month() ) {
  70. $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
  71. } elseif ( is_day() ) {
  72. $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
  73. } elseif ( is_post_type_archive() ) {
  74. $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  75. } elseif ( is_tax() ) {
  76. $tax = get_taxonomy( get_queried_object()->taxonomy );
  77. /* translators: %s: Taxonomy singular name. */
  78. $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
  79. } else {
  80. $title = __( 'Archives:', 'twentynineteen' );
  81. }
  82. return $title;
  83. }
  84. add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );
  85. /**
  86. * Add custom sizes attribute to responsive image functionality for post thumbnails.
  87. *
  88. * @origin Twenty Nineteen 1.0
  89. *
  90. * @param array $attr Attributes for the image markup.
  91. * @return string Value for use in post thumbnail 'sizes' attribute.
  92. */
  93. function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
  94. if ( is_admin() ) {
  95. return $attr;
  96. }
  97. if ( ! is_singular() ) {
  98. $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
  99. }
  100. return $attr;
  101. }
  102. add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
  103. /**
  104. * Add an extra menu to our nav for our priority+ navigation to use
  105. *
  106. * @param string $nav_menu Nav menu.
  107. * @param object $args Nav menu args.
  108. * @return string More link for hidden menu items.
  109. */
  110. function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
  111. if ( 'menu-1' === $args->theme_location ) :
  112. $nav_menu .= '
  113. <div class="main-menu-more">
  114. <ul class="main-menu">
  115. <li class="menu-item menu-item-has-children">
  116. <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
  117. aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
  118. twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
  119. </button>
  120. <ul class="sub-menu hidden-links">
  121. <li class="mobile-parent-nav-menu-item">
  122. <button class="menu-item-link-return">' .
  123. twentynineteen_get_icon_svg( 'chevron_left' ) .
  124. esc_html__( 'Back', 'twentynineteen' ) . '
  125. </button>
  126. </li>
  127. </ul>
  128. </li>
  129. </ul>
  130. </div>';
  131. endif;
  132. return $nav_menu;
  133. }
  134. add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
  135. /**
  136. * WCAG 2.0 Attributes for Dropdown Menus
  137. *
  138. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  139. * for flyout and dropdown menus.
  140. *
  141. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  142. */
  143. function twentynineteen_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  144. // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
  145. $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
  146. if ( $item_has_children ) {
  147. $atts['aria-haspopup'] = 'true';
  148. $atts['aria-expanded'] = 'false';
  149. }
  150. return $atts;
  151. }
  152. add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 4 );
  153. /**
  154. * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
  155. *
  156. * This duplicates each parent nav menu item and makes it the first child of itself.
  157. *
  158. * @param array $sorted_menu_items Sorted nav menu items.
  159. * @param object $args Nav menu args.
  160. * @return array Amended nav menu items.
  161. */
  162. function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
  163. static $pseudo_id = 0;
  164. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  165. return $sorted_menu_items;
  166. }
  167. $amended_menu_items = array();
  168. foreach ( $sorted_menu_items as $nav_menu_item ) {
  169. $amended_menu_items[] = $nav_menu_item;
  170. if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
  171. $parent_menu_item = clone $nav_menu_item;
  172. $parent_menu_item->original_id = $nav_menu_item->ID;
  173. $parent_menu_item->ID = --$pseudo_id;
  174. $parent_menu_item->db_id = $parent_menu_item->ID;
  175. $parent_menu_item->object_id = $parent_menu_item->ID;
  176. $parent_menu_item->classes = array( 'mobile-parent-nav-menu-item' );
  177. $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
  178. $amended_menu_items[] = $parent_menu_item;
  179. }
  180. }
  181. return $amended_menu_items;
  182. }
  183. add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );