• I’ve got a CMS-style wordpress setup where the homepage is not the main blog posts page.

    However, I would like to display the first paragraph of the latest blog post on the homepage, with a link to the post. How would I do that?

    I’d also like to display, above this excerpt, the first image within the post, but I’m guessing that might be harder… or I’ll have to use the ‘featured image’ or a custom field…?

Viewing 1 replies (of 1 total)
  • The code below is totally UNTESTED and unstyled, but should be close to what you want. Place it ahead of the mail loop in your template.

    <?php
     global $post;
     $myposts = get_posts('numberposts=1');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       <?php echo get_the_post_thumbnail($post->ID);
       the_excerpt();
    endforeach; ?>

    You can get the first image with a call to get_the_post_thumbnail().

    If you need more control, you can use the function below:

    <?php # Displays post image attachment (sizes: thumbnail, medium, full)
    function dp_attachment_image($postid=0, $size='thumbnail', $attributes='', $echo=1,       $beforeimg='', $afterimg='', $numberposts=1) {
       if ($postid<1) $postid = get_the_ID();
       if ($images = get_children(array(
          'post_parent' => $postid,
          'post_type' => 'attachment',
          'numberposts' => $numberposts,
          'post_mime_type' => 'image',)))
          foreach($images as $image) {
             $img = $beforeimg;
             $attachment=wp_get_attachment_image_src($image->ID, $size);
             $img .= "<img src='{$attachment[0]}' $attributes />";
             $img .= $afterimg;
             if ($echo) {
               echo $img;
             } else {
               return $img;
             }
          }
    }?>
Viewing 1 replies (of 1 total)
  • The topic ‘How to display an excerpt of the latest post on the homepage?’ is closed to new replies.