Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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>';
}
share|improve this answer
    
thanks for the help! –  helgatheviking May 10 '12 at 17:00
add comment

@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();.

share|improve this answer
    
Smart solution with length x 1.2. +1 –  kaiser May 10 '12 at 9:34
    
agreed, +1 for the length comparison, that's sort of what i thought i wanted, but post formats were indeed the right way to go –  helgatheviking May 10 '12 at 17:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.