• Resolved coke

    (@coke)


    Hi there,
    I would like to echo a list of 3 attachments from a page into my content-page.php file.

    <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 have a snippet of code above that allows me to call them all together as a list, but the
    problem is I would need them separately.

    Any ideas?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Since $attachments is an array, you could grab them individually via their index, e.g., $attachments[0], $attachments[1] and $attachments[2].

    That said, can you clarify why you need them separately? What are you looking to accomplish?

    Thread Starter coke

    (@coke)

    Sure,
    essentially I am trying to achieve this effect:
    http://www.arriere-garde.com/wp-content/uploads/2014/12/ffffff.png

    which I have done with a grid system called Toast. The problem is I have linked the images directly within the php file like so:

    <div class="grid">
      <div class="grid__col grid__col--2-of-6">
         <img src="unnamed-3.jpg" alt="" />
      </div>
      <div class="grid__col grid__col--3-of-6">
            <img src="unnamed-5.jpg" alt="" />
         </div>
       <div class="grid__col grid__col--3-of-5">
          <img src="unnamed-1.jpg" alt="" />
       </div>
    </div>

    However, I would like it to be a little less “manual” and make it so the three images are 3 attachments from the post/page.

    Do you need control over which image is in what position (considering some images have a different orientation)? Will it be the same for every page or could it be different? Might you have more images attached to a page than you wish to show in the future?

    Thread Starter coke

    (@coke)

    I did consider that but if there’s not a solution for the control it’s not a train smash.

    It’s an extremely simple website and there will only be one page.

    Regarding the number, for now I think there will only be 3. If there are any more in the future, I might consider going up to a maximum of 5 or incorporating some sort of slider (or three sliders) in the space of the three images, so that each time you click an image it will slide, if that makes any sense

    Yes, that makes perfect sense.

    So, if it’s just one page and not something that’s going to continue to grow much, then I’d keep it simple and just reference the images by their index. It does, after all, get the job done.

    If you’re going to continue adding pages like that or need more control, you might consider creating a shortcode, much like how the WordPress gallery works, but your own version, which gives you control of the order, which of the attached images appear, and how the markup will be built.

    For example:

    [collage ids="12,16,83"]

    Another solution might be to create a custom post type called collage, in which you can group images and then you can just reference it on any page via something like:

    [collage id="76"]

    Thread Starter coke

    (@coke)

    Thank you so much! I will certainly do that out if it grows furthur.

    I can’t seem to reference the images by their index like you suggested, could I bother you for an example of code on how you’d do it?

    Try something like this:

    <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 ( count($attachments) === 3 ): ?>
        <div class="grid">
          <div class="grid__col grid__col--2-of-6">
            <?php echo wp_get_attachment_image( $attachments[0]->ID, 'full' ); ?>
          </div>
          <div class="grid__col grid__col--3-of-6">
            <?php echo wp_get_attachment_image( $attachments[1]->ID, 'full' ); ?>
          </div>
          <div class="grid__col grid__col--3-of-5">
            <?php echo wp_get_attachment_image( $attachments[2]->ID, 'full' ); ?>
          </div>
        </div>
      <?php endif;
      endwhile; endif; ?>
    </ul>
    Thread Starter coke

    (@coke)

    perfect. Thank you so much!

    Sure, no problem! Once you’ve confirmed this works, go ahead and mark this thread as resolved.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Get attachments individually’ is closed to new replies.