0

I am using the code below for showing featured image in single post content.It's working fine.But when I call the_excerpt(); in any template it showing featured image with post content.I want it show featured image only single post.Please help!

add_filter( 'the_content', 'put_thumbnail_in_posting' ); 
function put_thumbnail_in_posting($content) { 
    global $post; 
    if ( has_post_thumbnail() && ( $post->post_type == 'post' ) ) {
        the_post_thumbnail( '',
            array(
                'style' => 'padding: 6px;
                margin-bottom: 8px;
                margin-right: 8px;
                max-width: 100%;
                border: 1px solid rgb(204, 204, 204);
                background: none repeat scroll 0% 0% rgb(238, 238, 238);'
            )
        );
    }

    return $content;
}
  • I gather you want to display your post thumbnail in your RSS feed as well? – Brad Dalton May 31 '14 at 14:48
1

Use an is_single() condition to ensure the thumbnail isn't added on archive pages. You should also be using get_the_post_thumbnail and prepending it to $content, rather than just echo'ing it out:

function put_thumbnail_in_posting( $content ) { 
    if ( is_single() && has_post_thumbnail() && get_post_type() == 'post' ) {
        $thumbnail = get_the_post_thumbnail( null, '',
            array(
                'style' => implode( ' ',
                    array(
                        'padding: 6px;',
                        'margin-bottom: 8px;',
                        'margin-right: 8px;',
                        'max-width: 100%;',
                        'border: 1px solid rgb(204, 204, 204);',
                        'background: none repeat scroll 0% 0% rgb(238, 238, 238);',
                    )
                )
            )
        );

        $content = $thumbnail . $content;
    }

    return $content;
}
1
 add_filter( 'the_content', 'put_thumbnail_in_posting' ); 
 function put_thumbnail_in_posting( $content ) { 
    if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
        $thumbnail = get_the_post_thumbnail( null, '',
            array(
                'style' => implode( ' ',
                    array(
                        'padding: 6px;',
                        'margin-bottom: 8px;',
                        'margin-right: 8px;',
                        'max-width: 100%;',
                        'border: 1px solid rgb(204, 204, 204);',
                        'background: none repeat scroll 0% 0% rgb(238, 238, 238);',
                    )
                )
            )
        );

        $content = $thumbnail . $content;
    }

    return $content;
}

This code includes the is_singular('post') conditional tag which controls the display of the featured image in single posts only.

the_content hook filters the_content, gets the post thumbnail if one has been added to the post using the featured image module and displays it then adds the content after the image.

You could code this differently and also add a fallback featured image in your child themes images folder which displays if one isn't added manually.

 add_filter( 'the_content', 'put_thumbnail_in_posting' ); 
 function put_thumbnail_in_posting( $content ) { 
    if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
        $thumbnail = get_the_post_thumbnail( null, '',
            array(
                'style' => implode( ' ',
                    array(
                        'padding: 6px;',
                        'margin-bottom: 8px;',
                        'margin-right: 8px;',
                        'max-width: 100%;',
                        'border: 1px solid rgb(204, 204, 204);',
                        'background: none repeat scroll 0% 0% rgb(238, 238, 238);',
                    )
                )
            )
        );

        $content = $thumbnail . $content;

        }

        else echo'<img src="' . get_stylesheet_directory_uri() . '/images/fallback.jpg' . '" alt="" />';

    return $content;
}

The 2nd code block includes a fallback featured image.

Here's another way to code it:

 add_filter( 'the_content', 'featured_image_before_content' ); 

 function featured_image_before_content( $content ) { 
    if ( is_singular('post') && is_main_query() && has_post_thumbnail()) {
        $thumbnail = get_the_post_thumbnail();

        $content = $thumbnail . $content;

        }

    return $content;
}
0

If featured Image not working. Dont worry you can download this plugin install it and use it. i works easily for you. visit for more tutorials about wordpress and seo

  • Please provide an short relevant description and code for example from the link given. – Pieter Goosen Jun 2 '14 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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