extras.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TextBook
  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 textbook_body_classes( $classes ) {
  16. // Adds a class of header-image when a header image exists
  17. if ( get_header_image() ) {
  18. $classes[] = 'header-image';
  19. }
  20. // Adds a class of group-blog to blogs with more than 1 published author.
  21. if ( is_multi_author() ) {
  22. $classes[] = 'group-blog';
  23. }
  24. // Adds a class of hfeed to non-singular pages.
  25. if ( ! is_singular() ) {
  26. $classes[] = 'hfeed';
  27. }
  28. // Add a class of no-sidebar when there is no sidebar present
  29. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  30. $classes[] = 'no-sidebar';
  31. }
  32. return $classes;
  33. }
  34. add_filter( 'body_class', 'textbook_body_classes' );
  35. /**
  36. * Add descriptions to menu items
  37. *
  38. * src: https://www.binarymoon.co.uk/2015/04/adding-menu-descriptions-to-wordpress-menus/
  39. *
  40. * @return string
  41. */
  42. function textbook_nav_description( $item_output, $item, $depth, $args ) {
  43. if ( 'header' == $args->theme_location && $item->description ) {
  44. $item_output = str_replace( $args->link_after . '</a>', '<span class="menu-item-description">' . $item->description . '</span>' . $args->link_after . '</a>', $item_output );
  45. }
  46. return $item_output;
  47. }
  48. add_filter( 'walker_nav_menu_start_el', 'textbook_nav_description', 10, 4 );
  49. /**
  50. * Add featured image as background image to hero.
  51. *
  52. * @see wp_add_inline_style()
  53. */
  54. function textbook_header_image_background() {
  55. global $wp_query;
  56. global $post;
  57. if ( get_header_image() ) {
  58. $css = '.site-header { background-image: url(' . esc_url( get_header_image() ) . '); }';
  59. wp_add_inline_style( 'textbook-style', $css );
  60. }
  61. }
  62. add_action( 'wp_enqueue_scripts', 'textbook_header_image_background' );