Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Konrad Pilch
Konrad Pilch
2,435 Points

Not working properly

HI,

So i wrote this code

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

and when i get it in my recent posts , it shows me only the image , the first image on my blog. I want it to show the image relevant , basically the same image it was put on the blog as main image. How can i do this?

Colin Marshall
Colin Marshall
32,861 Points

Are you calling this inside of "the loop?"

Konrad Pilch
Konrad Pilch
2,435 Points
 <!-- Lates Posts -->
                    <div id="bl_latest_posts" class="box">
                        <h3 class="widget-head">Read the latest</h3>

                        <h3 class="bl_subtitle">Posts</h3>

                        <div class="tab-content">
                            <div class="tab-panels">
                                <div class="wrapper-post-sidebar">
                                    <ul class="tab-ul">

                                        <?php
                                            $args = array( 'numberposts' => '5' );
                                            $recent_posts = wp_get_recent_posts( $args );
                                            foreach( $recent_posts as $recent ){ ?>


                                            <!-- Good code <?php //echo get_the_post_thumbnail( $post_id, $size, $attr ); ?> -->

                                            <!-- Post -->
                                            <li style="right: 0px">
                                                <div class="wrapper-post-sidebar row">
                                                    <div class="col-xs-2">
                                                        <a class="tab-attachment" href="">
                                                            <?php echo get_the_post_thumbnail($post_id); ?>
                                                        </a>
                                                    </div><!-- /col-md-2 -->

                                                    <div class="col-xs-10">
                                                        <div class="tab-text">
                                                            <h5><?php echo $recent["post_title"] ?>

                                                            <small class="entry-date" datetime=""><?php echo get_the_date( $format, $post_id ); ?></small>
                                                            </h5>
                                                        </div><!-- /tab-text -->
                                                    </div><!-- /col-md-10 -->
                                                </div><!-- /row -->
                                            </li>
                                        <?php } ?>

                                    </ul>
                                </div>
                            </div><!-- /tab-panels -->
                        </div><!-- /tab-content -->
                    </div><!-- /latest-post -->
Konrad Pilch
Konrad Pilch
2,435 Points

ene this

<?php

if ( function_exists( 'add_image_size' ) ) {
  add_image_size( 'custom-thumb', 180, 115, true ); //add a custom image size
}

echo get_the_post_thumbnail(get_the_ID(), 'custom-thumb', $attr); //echo the thumbnail with the new custom image size

?>