-1

I've been googling this with no result.

I have a bunch of posts. But all of them does not have a featured image. So I was thinking it would look much nicer if all posts still had the same height.

Is it possible to display a longer excerpt if there is no featured image in the post?

Okey so this is what the code looks like now:

 /**
 * Set the post excerpt length to 40 words.
 * @param int $length The number of excerpt characters.
 * @return int The filtered number of characters.
 */

function twentyeleven_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );

Maybe some sort of if statement could do the trick? You know:

if (featuredimage = true) {
function twentyeleven_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
}
else {function twentyeleven_excerpt_length( $length ) {
return 80;
}
add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );}

As you can see I am a total newbie at this so I need some guidance.

Cami
  • 79
  • 1
  • 2
  • 9
  • Are you asking if there's an existing option in Wordpress to do this, or are you wanting to program something to do it? – Peter Bloomfield Nov 08 '13 at 14:33
  • 2
    Questions must describe the specific problem — and include valid code to reproduce it — in the question itself. – Kermit Nov 08 '13 at 14:34
  • please edit the question (or create a pastebin) with the content of the PHP that you can find in the php file of your theme that is responsible for the homepage – Pikk Nov 08 '13 at 14:37
  • 1
    @Pikk No, don't make a Pastebin, but put the code in the question: if the Pastebin vanishes, the question gets useless. – Marcel Korpel Nov 08 '13 at 14:38

2 Answers2

0

You can put solution together by

  1. Creating a custom excerpt function to make longer excerpts as shown in this answer and saving it in your functions file
  2. Using has_post_thumbnail to see if the post has a thumbnail/featured image, then serve up the correct excerpt accordingly:

    <?php
    if ( has_post_thumbnail() ) {
    the_post_thumbnail();
        echo excerpt(25); // Short excerpt
    } else {
        echo excerpt(50); // Long excerpt
    }
    ?>
    

Bear in mind that excerpts are trimmed according to word length; therefore you can't guarantee the end height. You should probably also look at using min-height in order to get the height you're after.

Community
  • 1
  • 1
Dre
  • 2,933
  • 2
  • 18
  • 21
  • Thank you I'll see what I can do with this.. The height doesn't have to be perfect so increasing the excerpt with some more words would look great. – Cami Nov 08 '13 at 15:04
  • Thank you so much. I added the code below just under the original excerpt in funcions.php. Works like a charm! function my_excerpt_length($length) { if (has_post_thumbnail()) { return 40; } else { return 80; } } add_filter('excerpt_length', 'my_excerpt_length'); – Cami Nov 11 '13 at 08:46
0

Use the Advanced Excerpt plugin. Use the has_post_thumbnail() conditional to test for whether the post has a featured image - if it does, display it + short excerpt (see usage guide for the plugin, you can limit by word count, character count, finish words/sentences and so forth), if it doesn't, display a longer excerpt. You just use the_advanced_excerpt() template tag and pass in the desired parameters.

Ennui
  • 10,102
  • 3
  • 35
  • 42