Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Filter the_excerpt to show content in a more ideal way.
- * https://stevengliebe.com/2015/02/26/excerpts-and-the-more-tag-in-wordpress-themes/
- *
- * The way content is shown depends on the priority of a situation:
- *
- * 1. the_content with "Read More" link if the "More" tag is used
- * 2. Manual excerpt with "Read More" link if excerpt is manually entered
- * 3. the_content in its entirety if less than 200 words (nicer than automatic excerpt)
- * 4. Automatic excerpt with "Read More" link if no better situation
- *
- * This is based on the handling of excerpts in churchthemes.com themes with improvements inspired
- * by Devin Price and commenters: http://wptheming.com/2015/01/excerpt-versus-content-for-archives/
- *
- * @global object $post
- * @param string $excerpt Excerpt from get_the_excerpt()
- * @return string Modified excerpt
- *
- * Modified so #1 above is not applied on the Home page
- * Modified to remove post title from Read More text
- */
- function themeslug_the_excerpt( $excerpt ) {
- global $post;
- $show_excerpt = false;
- // Remove post title from "Read More" text
- /* translators: %s is post title (hidden, for screen readers) */
- /*
- $more_text = sprintf(
- __( 'Read More %s', 'themeslug' ),
- the_title( '<span class="screen-reader-text">— ', '</span>', false )
- */
- $more_text = sprintf(
- __( 'Read More', 'themeslug' ),
- the_title( '<span class="screen-reader-text">— ', '</span>', false )
- );
- // 1. the_content with "Read More" link if the "More" tag is used, but not password-protected
- // This is first because it is as intentional as manual excerpt but manual excerpt could have other purpose (ie. meta description)
- // Modified so not applied on the Home page
- if ( (! is_home()) && strpos( $post->post_content, '<!--more-->' ) && ! post_password_required() ) {
- the_content( $more_text );
- }
- // 2. Manual excerpt with "Read More" link if excerpt is manually provided
- elseif ( has_excerpt() ) {
- $show_excerpt = true;
- }
- // 3. the_content in its entirety if less than 200 words, but not password-protected (nicer than automatic excerpt)
- // A nice-looking fallback for when they do not use "more" or manual excerpt, or if only published a media embed
- elseif ( str_word_count( $post->post_content ) < 200 && ! post_password_required() ) {
- the_content();
- }
- // 4. Automatic excerpt if no better situation
- else {
- $show_excerpt = true;
- }
- // Show excerpt with "Read More"
- if ( $excerpt && $show_excerpt ) {
- ?>
- <?php
- // Manual or automatic excerpt
- echo $excerpt;
- ?>
- <p>
- <?php
- // "Read More" linked
- printf(
- '<a href="%1$s" class="more-link" rel="bookmark">%2$s</a>',
- esc_url( get_permalink() ),
- $more_text
- );
- ?>
- </p>
- <?php
- }
- }
- add_filter( 'the_excerpt', 'themeslug_the_excerpt' );
Advertisement
Add Comment
Please, Sign In to add comment