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.

I have a problem with displaying some content on my homepage. First i'm going to explain my problem. I have a page with all my blog posts on it (use of excerpt function). When you click on a read more link on the blog posts page, you get the whole article. On my homepage I want to show the 3 latest posts. I already have the top 3 off the posts. (with the use of the excerpt function) but these teasers or to long. There is to much text. I don't want to make it shorter because when I do this, the page with all the blogposts is also going to change. I was thinking to use the custom fields in the blog posts. Then I can make an extra field for the teaser content on the homepage? Is this the right solution or is there something else?

share|improve this question
1  
This is a difficult question to understand. You should consider formmating your question using paparagraphs and the wysiwyg tools –  Blowsie Jan 31 '13 at 16:57
    
I don't think <custom-post-type> is the right tag for this question. –  Simon Jan 31 '13 at 17:18

4 Answers 4

I think what your trying to ask is.

How can i have multiple excerpt lengths?

See this similar question on StackOverflow

http://stackoverflow.com/questions/4082662/multiple-excerpt-lengths-in-wordpress

share|improve this answer

Custom Fields are perfect for this when you want not different excerpt lenght, but also different excerpt content.

Following code will add additional TinyMCE editor to the Edit Post admin page so you can write formatted excerpt specially for your front page. Excerpt will be stored in _wpse83958_front_page_excerpt custom field. Preceding underscore makes it invisible in Custom Fields metabox.

Put it to your functions.php:

// Do not use 'add_meta_boxes' with TinyMCE
add_action( 'edit_form_advanced', 'wpse83958AddFrontPageExcerptEditor' );
add_action( 'save_post', 'wpse83958SaveFrontPageExcerpt' );

function wpse83958AddFrontPageExcerptEditor() {
    global $post;

    wp_nonce_field( 'wpse83958_front_page_excerpt', 'wpse83958_front_page_excerpt_noncename' );

    echo '<h2>Front page excerpt</h2>';

    // 'textarea_name' argument is necessary because of 'editor_id' parameter limitations (http://codex.wordpress.org/Function_Reference/wp_editor#Parameters)
    // See http://core.trac.wordpress.org/ticket/19173 for information about Editor moving (drag'n'drop)

    // second argument ($editor_id) must contain lowercase letters ONLY ([a-z])
    wp_editor(
        get_post_meta($post->ID, '_wpse83958_front_page_excerpt', true),
        'wpse83958frontpageexcerpt',
        array(
            'textarea_name'     => 'wpse83958_front_page_excerpt',
            'wpautop'           => true
        )
    );
}


function wpse83958SaveFrontPageExcerpt( $post_id ) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    if( isset($_POST['wpse83958_front_page_excerpt']) && !wp_verify_nonce( $_POST['wpse83958_front_page_excerpt_noncename'], 'wpse83958_front_page_excerpt' ) )
        return;

    if ( !current_user_can( 'edit_page', $post_id ) || !current_user_can( 'edit_post', $post_id ) )
        return;

    $wpse83958_front_page_excerpt = $_POST['wpse83958_front_page_excerpt'];

    if('' != $wpse83958_front_page_excerpt)
        update_post_meta($post_id, '_wpse83958_front_page_excerpt', $wpse83958_front_page_excerpt);
    else
        delete_post_meta($post_id, '_wpse83958_front_page_excerpt');

}

And get your excerpt inside front page loop:

$front_page_excerpt = get_post_meta($post->ID, '_wpse83958_front_page_excerpt', true);
// Change double linebreaks to '<p>' elements as only 'the content' and 'the excerpt' do use them automatically
$front_page_excerpt = wpautop($front_page_excerpt);

echo $front_page_excerpt;
share|improve this answer

No need for custom fields. WordPress already has this - the Excerpt meta box. On WP3.2+ this is usually hidden by default. To display this text box, open the Screen Options found in the top right corner of the post edit screen, then look for the excerpt checkbox and enable it. You should now see an Excerpt text box somewhere below the content editor. Write your custom excerpt here.

share|improve this answer
    
Thank you for your answer but When I add this option, all the excerpts are changing. I only want to change the excerpts of the top 3 on the frontpage. –  Niels Jansen Jan 31 '13 at 18:23
    
hmmmm.. so, you only want to show the custom excerpts on the homepage only? –  WP Themes Jan 31 '13 at 21:40

By using a custom field, you will be able to use a custom text instead of just a truncated text, which is best for SEO : you could write real teasers. But, it's also more work when publishing.

If there is not point, you you just need to change the lenght of the except, I would use one built-in functions of WP which is wp_trim_words (http://codex.wordpress.org/Function_Reference/wp_trim_words).

I would propose this structure :

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
static $firsts = 0; ?>
<section>
    <h1><?php the_title(); ?></h1>
    <article>
        <? if ( $firsts < 3 ) { 
            $extrait =  $post->post_excerpt;
            if ( $extrait ) {
                echo wp_trim_words( $extrait, 10, '[...]' ); // 10 words 
            } else {
                $extrait = strip_tags ( get_the_content() ); // if no excerpt, use content
                echo wp_trim_words( $extrait, 10, '[...]' ); // 10 words 
            }
        } else {
            the_excerpt();
        } ?>
    </article>
</section>
<? $firsts++; endwhile; endif; ?>

So you could have an specificly written excerpt using the dedicated field on a post, cut to a specific number for the first three posts, and the automatic excerpt for the rest of the loop.

Hope this helps. Regards

EDIT I changed the original code proposition because get_the_excerpt() is never empty (according to codex, it returns a empty string with [...] at the end) so I use the $post->post_excerpt in place. Tx to Niels Jansen for pointing this out.

LAST EDIT Referring to the codex, the_excerpt() always returns something:

If you do not provide an explicit excerpt to a post (in the post editor's optional excerpt field), it will display an automatic excerpt which refers to the first 55 words of the post's content.

In practise: When you post a new post, I assume there is always content? Why should we blog otherwise? So this is the code I will maybe use:

$extrait =  get_the_excerpt();
     if ($extrait)
     {
         echo wp_trim_words($extrait, 20, '...');
     }
     ?>

EDIT AGAIN I have try to introduce the idea of having the first three post having a different excerpt from the rest of the loop. We could have use offset, too.

share|improve this answer
1  
Simon, your variable $extrait will always be something, so we never came to the else part. If i just echo the $extrait, i always see something. –  Niels Jansen Jan 31 '13 at 23:00
    
Thank you Niels to pointing that out. I made a change that I suppose is correct inside the loop. –  Simon Jan 31 '13 at 23:15
    
Simon, I think there is a lot of code not necessary. I will try to explain in the post above. –  Niels Jansen Jan 31 '13 at 23:21
    
I edit again because the question was to change the excerpt only for the three first posts. Thanks for you edit. –  Simon Feb 1 '13 at 0:04

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.