SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get first image into post

    Hello

    the following is taken from the wordpress codex:

    Examples
    Display all images as a list

    To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:

    Code:
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
    
     $args = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      );
    
      $attachments = get_posts( $args );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<li>';
               echo wp_get_attachment_image( $attachment->ID, 'full' );
               echo '<p>';
               echo apply_filters( 'the_title', $attachment->post_title );
               echo '</p></li>';
              }
         }
    
     endwhile; endif; ?>
    </ul>

    I am looking to use similar code to only display the full size of the first image from the gallery of the post.

    Any ideas?

    many thanks,
    Andy

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neodjandre View Post
    Hello

    the following is taken from the wordpress codex:

    Examples
    Display all images as a list

    To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:

    Code:
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
    
     $args = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      );
    
      $attachments = get_posts( $args );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<li>';
               echo wp_get_attachment_image( $attachment->ID, 'full' );
               echo '<p>';
               echo apply_filters( 'the_title', $attachment->post_title );
               echo '</p></li>';
              }
         }
    
     endwhile; endif; ?>
    </ul>

    I am looking to use similar code to only display the full size of the first image from the gallery of the post.

    Any ideas?

    many thanks,
    Andy
    Do a var_dump on $attachments for us. All it comes down to is making sure that $attachments is always ordered the same way to make sure your always grabbing the first correct one. Your code would become this:

    Code PHP:
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
     
     $args = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      );
     
      $attachments = get_posts( $args );
         if ( $attachments ) {
               echo '<li>';
               echo wp_get_attachment_image( $attachments[0]->ID, 'full' );
               echo '<p>';
               echo apply_filters( 'the_title', $attachments[0]->post_title );
               echo '</p></li>';
         }
     
     endwhile; endif; ?>
    </ul>

    I'm just simply going straight to the first item in the array, and removing the for each loop.
    Last edited by K. Wolfe; Feb 5, 2013 at 15:28. Reason: Formatting

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Kyle.. this is helpful


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •