extras.php 4.4 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 Spun
  8. */
  9. /**
  10. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  11. *
  12. */
  13. function spun_page_menu_args( $args ) {
  14. $args['show_home'] = true;
  15. return $args;
  16. }
  17. add_filter( 'wp_page_menu_args', 'spun_page_menu_args' );
  18. /**
  19. * Adds custom classes to the array of body classes.
  20. *
  21. */
  22. function spun_body_classes( $classes ) {
  23. // Adds a class of group-blog to blogs with more than 1 published author
  24. if ( is_multi_author() ) {
  25. $classes[] = 'group-blog';
  26. }
  27. return $classes;
  28. }
  29. add_filter( 'body_class', 'spun_body_classes' );
  30. /**
  31. * Filter in a link to a content ID attribute for the next/previous image links on image attachment pages
  32. *
  33. */
  34. function spun_enhanced_image_navigation( $url, $id ) {
  35. if ( ! is_attachment() && ! wp_attachment_is_image( $id ) )
  36. return $url;
  37. $image = get_post( $id );
  38. if ( ! empty( $image->post_parent ) && $image->post_parent != $id )
  39. $url .= '#main';
  40. return $url;
  41. }
  42. add_filter( 'attachment_link', 'spun_enhanced_image_navigation', 10, 2 );
  43. /**
  44. * Remove widget title header if no title is entered.
  45. * @todo Remove this function when this is fixed in core.
  46. */
  47. function spun_calendar_widget_title( $title = '', $instance = '', $id_base = '' ) {
  48. if ( 'calendar' == $id_base && '&nbsp;' == $title )
  49. $title = '';
  50. return $title;
  51. }
  52. add_filter( 'widget_title', 'spun_calendar_widget_title', 10, 3 );
  53. /**
  54. * Filter archives to display one less post per page to account for the .page-title circle
  55. */
  56. function spun_limit_posts_per_archive_page() {
  57. if ( ! is_home() && is_archive() || is_search() ) {
  58. $posts_per_page = intval( get_option( 'posts_per_page' ) ) - 1;
  59. set_query_var( 'posts_per_page', $posts_per_page );
  60. }
  61. }
  62. add_filter( 'pre_get_posts', 'spun_limit_posts_per_archive_page' );
  63. /**
  64. * Count the number of active sidebars and generate an ID to style them.
  65. */
  66. function spun_count_sidebars() {
  67. $count = 0;
  68. if ( is_active_sidebar( 'sidebar-1' ) || is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-3' ) )
  69. $count = 'one';
  70. if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) ||
  71. is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-3' ) ||
  72. is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) )
  73. $count = 'two';
  74. if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) )
  75. $count = 'three';
  76. print $count;
  77. }
  78. /**
  79. * For image post format, as we display the featured image properly,
  80. * regex it out of the content.
  81. */
  82. function spun_strip_featured_image( $content ) {
  83. if ( '' != get_the_post_thumbnail() && 'image' == get_post_format() ) {
  84. $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
  85. $image_url = $image_url[0];
  86. $image_url = str_replace( array( 'http://', 'https://' ), 'https?://', $image_url );
  87. $regex = sprintf( '#<a href="%1$s"><img[^>]*? src="%1$s[^"]*" [^>]*?/></a>#', $image_url );
  88. $content = preg_replace( $regex, '', $content );
  89. }
  90. return $content;
  91. }
  92. add_filter( 'the_content', 'spun_strip_featured_image' );
  93. /**
  94. * Remove the 1st gallery shortcode from gallery post format content.
  95. */
  96. function spun_strip_first_gallery( $content ) {
  97. if ( 'gallery' == get_post_format() && 'post' == get_post_type() ) {
  98. $regex = '/\[gallery.*]/';
  99. $content = preg_replace( $regex, '', $content, 1 );
  100. }
  101. return $content;
  102. }
  103. add_filter( 'the_content', 'spun_strip_first_gallery' );
  104. /**
  105. * Filters wp_title to print a neat <title> tag based on what is being viewed.
  106. *
  107. * @param string $title Default title text for current view.
  108. * @param string $sep Optional separator.
  109. * @return string The filtered title.
  110. */
  111. function spun_wp_title( $title, $sep ) {
  112. global $page, $paged;
  113. if ( is_feed() )
  114. return $title;
  115. // Add the blog name
  116. $title .= get_bloginfo( 'name' );
  117. // Add the blog description for the home/front page.
  118. $site_description = get_bloginfo( 'description', 'display' );
  119. if ( $site_description && ( is_home() || is_front_page() ) )
  120. $title .= " $sep $site_description";
  121. // Add a page number if necessary:
  122. if ( $paged >= 2 || $page >= 2 )
  123. $title .= " $sep " . sprintf( __( 'Page %s', 'spun' ), max( $paged, $page ) );
  124. return $title;
  125. }
  126. add_filter( 'wp_title', 'spun_wp_title', 10, 2 );