WordPress.org

Ready to get started?Download WordPress

Forums

latest post on index page (4 posts)

  1. amaeria
    Member
    Posted 4 years ago #

    Hey
    I wonder how to get the latest post from a specific category /news/ as my index page?
    Title and Photo from the post and when it is clicked i would like to come to all the posts in this category?

    cannot make it work so that the photo shows so now, again, how to do this?

  2. vtxyzzy
    Member
    Posted 4 years ago #

    If you are using a freely available theme, post a link to your site and someone may be able to help.

  3. amaeria
    Member
    Posted 4 years ago #

  4. vtxyzzy
    Member
    Posted 4 years ago #

    Add this function to your functions.php:

    <?php # Displays post image attachment (sizes: thumbnail, medium, full)
    function dp_attachment_image($postid=0, $size='thumbnail', $attributes='',$echo=1) {
       if ($postid<1) $postid = get_the_ID();
       if ($images = get_children(array(
          'post_parent' => $postid,
          'post_type' => 'attachment',
          'numberposts' => 1,
          'post_mime_type' => 'image',)))
          foreach($images as $image) {
             $attachment=wp_get_attachment_image_src($image->ID, $size);
             $img = "<img src='{$attachment[0]}' $attributes />";
             if ($echo) {
               echo $img;
             } else {
               return $img;
             }
          }
    }?>

    Use this to display your attachment:

    <?php
    $cat = 39;  // Replace this with your own category id
    query_posts("posts_per_page=1&caller_get_posts=1&cat=$cat");
    if (have_posts()) : while (have_posts()) : the_post();
       $link = get_category_link($cat);
       $echo = 0;
       $img = dp_attachment_image($post->ID,'full','width=90%',$echo);
       if ($img) { ?>
          <h2><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h2>
          <a href="<?php echo $link; ?>" > <?php echo $img; ?> </a>
       <?php }
     endwhile;
    endif;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic