0

I am using the following code to display the posts on my home page.

But I could not print thumbnails using get_the_post_thumbnail($post->ID, 'thumbnail');

any help to solve this will be very much appreciated..

while (have_posts()) : the_post();
                $excerpt = get_the_excerpt();   
                $date = explode(",", get_the_time("F,j,Y", "", "", false));
                print(' 
                    <section class="large-4 columns">
                    <section class="main_post cbox1">
                    <section class="post_inner row collapse">');             
                    print('<div class="large-2 columns small-2">
                                <span class="date_format">
                                    <span class="date_num">' . $date[1] . '
                                    </span><br/><span class="date_month">' . substr($date[0], 0, 3) . '
                                    </span></span></span>
                                </span>
                            </div>');
                    print ('<div class="large-10 columns small-10">');
                            the_title('<a class="blogroll_title" href="' . get_permalink() . '"><h1 class="heading_blog">', '</h1></a>');
                    print ('</div>');                                   
                    print ('<!--end post_inner --></section>');
                    print ('<div class="row collapse post_content"><p>');
                        if (strlen(get_the_content()) > 100) {
                            $content = get_the_content();
                            echo str_replace('&nbsp;', '<br /><br />', substr($content, 0, 300)). "....";   
                            echo '</p><a href=" ' . get_permalink() . ' " class="read_more button button_top button_body_col11 button_radius">Read More</a>';
                            echo get_the_post_thumbnail($post->ID, 'thumbnail'); // post image thumbanails
                        }
                        else { the_content(); echo "</p>"; }

** if I use the feature image then it prints twice. Thanks.

6
  • what do you mean by " if I use the feature image then it prints twice." Commented Jan 6, 2014 at 7:33
  • there is a "featured image" tab at the right side of my "post" editor. If I click "set Featured image" and add an image, that image thumbnail is shown at the home page Commented Jan 6, 2014 at 7:36
  • try replacing get_the_post_thumbnail($post->ID, 'thumbnail') to get_the_post_thumbnail( get_the_ID(), 'thumbnail')
    – Shazzad
    Commented Jan 6, 2014 at 7:42
  • 1
    @RiffazStarr This is how it is suppose to work, get_the_post_thumbnail gets the featured image. Check the codex for more details Commented Jan 6, 2014 at 7:48
  • @Shazzad thats not working for me. Commented Jan 6, 2014 at 9:09

1 Answer 1

1

The problem of showing the thumbnail twice is because of your the_content(); which is defined in the content-page.php file that has the line the_post_thumbnail(); that will get the featured image.

so intead of using the the_content() just use the get_the_content() function to get only the content without the featured image.

Let me know if that solves your problem.

2
  • Thanks for that @yamu It's solved now. I forget to remove the lines if(has_post_thumbnail()) { the_post_thumbnail(); } else{} I've ever added this to check it before. now the problem is these commands showing the thumbnail if the post image is featured. but I do not want to set the image to "FEATURED" every time. I just want to show the thumbnail if the post has any images.. any ideas? Commented Jan 6, 2014 at 9:38
  • Well that's not yet provided by wordpress to get any media in the content to show as thumbnail..
    – Yamu
    Commented Jan 6, 2014 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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