extras.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Canard
  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 canard_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. return $classes;
  21. }
  22. add_filter( 'body_class', 'canard_body_classes' );
  23. if ( version_compare( $GLOBALS['wp_version'], '4.1', '<' ) ) :
  24. /**
  25. * Filters wp_title to print a neat <title> tag based on what is being viewed.
  26. *
  27. * @param string $title Default title text for current view.
  28. * @param string $sep Optional separator.
  29. * @return string The filtered title.
  30. */
  31. function canard_wp_title( $title, $sep ) {
  32. if ( is_feed() ) {
  33. return $title;
  34. }
  35. global $page, $paged;
  36. // Add the blog name
  37. $title .= get_bloginfo( 'name', 'display' );
  38. // Add the blog description for the home/front page.
  39. $site_description = get_bloginfo( 'description', 'display' );
  40. if ( $site_description && ( is_home() || is_front_page() ) ) {
  41. $title .= " $sep $site_description";
  42. }
  43. // Add a page number if necessary:
  44. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  45. $title .= " $sep " . sprintf( __( 'Page %s', 'canard' ), max( $paged, $page ) );
  46. }
  47. return $title;
  48. }
  49. add_filter( 'wp_title', 'canard_wp_title', 10, 2 );
  50. /**
  51. * Title shim for sites older than WordPress 4.1.
  52. *
  53. * @link https://make.wordpress.org/core/2014/10/29/title-tags-in-4-1/
  54. * @todo Remove this function when WordPress 4.3 is released.
  55. */
  56. function canard_render_title() {
  57. ?>
  58. <title><?php wp_title( '|', true, 'right' ); ?></title>
  59. <?php
  60. }
  61. add_action( 'wp_head', 'canard_render_title' );
  62. endif;
  63. if ( ! function_exists( 'canard_excerpt_more' ) && ! is_admin() ) :
  64. /**
  65. * Replaces "[...]" (appended to automatically generated excerpts) with ...
  66. *
  67. * @since Canard 1.0.3
  68. */
  69. function canard_excerpt_more( $more ) {
  70. return ' &hellip;';
  71. }
  72. add_filter( 'excerpt_more', 'canard_excerpt_more' );
  73. endif;
  74. if ( ! function_exists( 'canard_continue_reading' ) && ! is_admin() ) :
  75. /**
  76. * Adds a "Continue reading" link to all instances of the_excerpt
  77. *
  78. * @since Canard 1.0.4
  79. *
  80. * @return string The excerpt with a 'Continue reading' link appended.
  81. */
  82. function canard_continue_reading( $the_excerpt ) {
  83. $the_excerpt = sprintf( '%1$s <a href="%2$s" class="more-link">%3$s</a>',
  84. $the_excerpt,
  85. esc_url( get_permalink( get_the_ID() ) ),
  86. /* translators: %s: Name of current post */
  87. sprintf( __( 'Continue reading %s', 'canard' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
  88. );
  89. return $the_excerpt;
  90. }
  91. add_filter( 'the_excerpt', 'canard_continue_reading', 9 );
  92. endif;
  93. /**
  94. * Custom lenght of the excerpt.
  95. */
  96. function canard_excerpt_length( $length ) {
  97. return 65;
  98. }
  99. add_filter( 'excerpt_length', 'canard_excerpt_length', 999 );
  100. /**
  101. * Returns the URL from the post.
  102. *
  103. * @uses get_the_link() to get the URL in the post meta (if it exists) or
  104. * the first link found in the post content.
  105. *
  106. * Falls back to the post permalink if no URL is found in the post.
  107. *
  108. * @return string URL
  109. */
  110. function canard_get_link_url() {
  111. $content = get_the_content();
  112. $has_url = get_url_in_content( $content );
  113. return ( $has_url && has_post_format( 'link' ) ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  114. }