Compare the_excerpt() to the_content()

Is there a way to compare the_excerpt() to the_content() to know if the_excerpt() is actually showing the entire post content? for instance, if a post were particularly short.

ultimately i’d like to have a “Read more” link at the end of excerpts. but i want it to say 1 thing for posts and another for posts of the video format (ie… ‘watch the video’ instead of ‘read the rest’). but at the same time i don’t want to manually tack this on after the excerpt, but i have plenty of posts that are short enough they don’t need a ‘read more’ link, since the_excerpt displays the full post.

Read More

but adding the permalink to the excerpt_more filter isn’t quite right since it won’t add a link to the video posts that have no other content.

so i’m stuck between the two. i hope that made sense. if it didn’t it’s late and i will try to re-explain in the morning.

Related posts

Leave a Reply

2 comments

  1. What you’re trying to do with the video is exactly what Post Formats were created to handle.

    Add this to functions:

    add_theme_support( 'post-formats', array( 'video' ) );
    

    And then this to handle your Read More link:

    if( !has_post_format( 'video' ) ) {
        echo '<a href="' . get_permalink() . '">Read More&hellip;</a>';
    } else {
        echo '<a href="' . get_permalink() . '">Watch the Video&hellip;</a>';
    }
    
  2. @mrwweb is right, post formats are very useful in most cases.

    As a more generic solution you could combine the_excerpt() and the_content() in one function:

    function wpse_51699_conditional_excerpt( $more_link_text = null, $stripteaser = false )
    {
        $excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );
    
        $content = get_the_content( $more_link_text, $stripteaser );
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]&gt;', $content);
    
        $stripped_content = strip_tags( $content );
        $content_length   = mb_strlen( $stripped_content, 'utf-8' );
        $excerpt_length   = mb_strlen( $excerpt, 'utf-8' );
    
        // $content is just 20% longer than excerpt. Adjust this to your needs.
        if ( ( $excerpt_length * 1.2 ) >= $content_length )
        {
            print $content;
            return;
        }
        echo $excerpt . $more_link_text;
    }
    

    In your theme you call now …

    wpse_51699_conditional_excerpt( sprintf( '<a href="%1$s">Read more</a>', get_permalink() ) );
    

    … instead of the_excerpt();.