WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show the_post_thumbnail() on home page | Latest posts. (3 posts)

  1. fldtrace
    Member
    Posted 4 years ago #

    I use this code on my home page to display the latest posts:

      <?php
      global $post;
      $tmp_post = $post;
      $myposts = get_posts('numberposts=5&offset=1&category=1');
      foreach($myposts as $post) :
      setup_postdata($post);
      ?>

    • "><?php the_title(); ?>
    • <?php endforeach; ?>
      <?php $post = $tmp_post; ?>

      <?php endforeach; ?>

    It shows right the permalink to the post but no image.
    My theme has the post thumbnail activated and works good on the posts page. I tried some methods, but I cannot pull the thumbnails. What do I need to add in the code from above?

    I really appreciate any help.

  2. fldtrace
    Member
    Posted 4 years ago #

    Here is the proper code:

    <ul>
     <?php
     global $post;
     $tmp_post = $post;
     $myposts = get_posts('numberposts=5&offset=1&category=1');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     <?php $post = $tmp_post; ?>
     </ul>
    <ul>
    <li class="latest_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></li>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <ul>
    <?php endforeach; ?>
  3. fldtrace
    Member
    Posted 4 years ago #

    Resolved. With default thumbnails, I forgot to attach thumbs for the posts in that category. The following code will generate the latest 5 posts from category with id=7. Will show up linked thumbnails and titles to each post:

    <?php
                     global $post;
                     $myposts = get_posts('numberposts=5&category=7');
                     foreach($myposts as $post) :
                     setup_postdata($post);
                     ?>
                         <ul class="latest clearfix">
                            <li class="latest_thumb">
    						<?php
    							if ( has_post_thumbnail() ) { ?>
    								<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    							<?php } else { ?>
    								<a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory') ;?>/i/news-img.jpg" alt=" "/></a>
    							<?php }
    						?>
                            </li>
                            <li class="latest_text">
                                <span class="date"><?php the_time('F j, Y'); ?></span>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </li>
                          </ul>
                     <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic