bdbrown

Filter Post Excerpt

Apr 15th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. /**
  2.  * Filter the_excerpt to show content in a more ideal way.
  3.  * https://stevengliebe.com/2015/02/26/excerpts-and-the-more-tag-in-wordpress-themes/
  4.  *
  5.  * The way content is shown depends on the priority of a situation:
  6.  *
  7.  * 1. the_content with "Read More" link if the "More" tag is used
  8.  * 2. Manual excerpt with "Read More" link if excerpt is manually entered
  9.  * 3. the_content in its entirety if less than 200 words (nicer than automatic excerpt)
  10.  * 4. Automatic excerpt with "Read More" link if no better situation
  11.  *
  12.  * This is based on the handling of excerpts in churchthemes.com themes with improvements inspired
  13.  * by Devin Price and commenters: http://wptheming.com/2015/01/excerpt-versus-content-for-archives/
  14.  *
  15.  * @global object $post
  16.  * @param string $excerpt Excerpt from get_the_excerpt()
  17.  * @return string Modified excerpt
  18.  *
  19.  * Modified so #1 above is not applied on the Home page
  20.  * Modified to remove post title from Read More text
  21.  */
  22. function themeslug_the_excerpt( $excerpt ) {
  23.  
  24.         global $post;
  25.  
  26.         $show_excerpt = false;
  27.  
  28.         // Remove post title from "Read More" text
  29.         /* translators: %s is post title (hidden, for screen readers) */
  30.     /*
  31.     $more_text = sprintf(
  32.                 __( 'Read More %s', 'themeslug' ),
  33.                 the_title( '<span class="screen-reader-text">&mdash; ', '</span>', false )
  34.         */
  35.     $more_text = sprintf(
  36.                 __( 'Read More', 'themeslug' ),
  37.                 the_title( '<span class="screen-reader-text">&mdash; ', '</span>', false )
  38.         );
  39.  
  40.         // 1. the_content with "Read More" link if the "More" tag is used, but not password-protected
  41.         // This is first because it is as intentional as manual excerpt but manual excerpt could have other purpose (ie. meta description)
  42.         // Modified so not applied on the Home page
  43.         if ( (! is_home()) && strpos( $post->post_content, '<!--more-->' ) && ! post_password_required() ) {
  44.                 the_content( $more_text );
  45.         }
  46.  
  47.         // 2. Manual excerpt with "Read More" link if excerpt is manually provided
  48.         elseif ( has_excerpt() ) {
  49.                 $show_excerpt = true;
  50.         }
  51.  
  52.         // 3. the_content in its entirety if less than 200 words, but not password-protected (nicer than automatic excerpt)
  53.         // A nice-looking fallback for when they do not use "more" or manual excerpt, or if only published a media embed
  54.         elseif ( str_word_count( $post->post_content ) < 200 && ! post_password_required() ) {
  55.                 the_content();
  56.         }
  57.  
  58.         // 4. Automatic excerpt if no better situation
  59.         else {
  60.                 $show_excerpt = true;
  61.         }
  62.  
  63.         // Show excerpt with "Read More"
  64.         if ( $excerpt && $show_excerpt ) {
  65.  
  66.                 ?>
  67.  
  68.                         <?php
  69.                         // Manual or automatic excerpt
  70.                         echo $excerpt;
  71.                         ?>
  72.  
  73.                         <p>
  74.                                 <?php
  75.                                 // "Read More" linked
  76.                                 printf(
  77.                                         '<a href="%1$s" class="more-link" rel="bookmark">%2$s</a>',
  78.                                         esc_url( get_permalink() ),
  79.                                         $more_text
  80.                                 );
  81.                                 ?>
  82.                         </p>
  83.  
  84.                 <?php
  85.  
  86.         }
  87.  
  88. }
  89. add_filter( 'the_excerpt', 'themeslug_the_excerpt' );
Advertisement
Add Comment
Please, Sign In to add comment