WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display latest 12 photos from media library (7 posts)

  1. pawciak
    Member
    Posted 1 year ago #

    Hey guys,

    I was looking for almost a week for such solution - without success. What I want to achieve is have a kind of 'photos cloud' on my wordpress homepage. What I need to do is to display latest 12 photos added to media library (so not from specific posts, but in general). thumbs should appear in a square - 3 cols, 4 rows (but of course this part I can get via css). I have no brightest idea how to make it..

    I've found such piece of code:

    $args = array(
            'post_type' => 'attachment',
            'post_status' => 'published',
            'posts_per_page' =>25,
            'post_parent' => 210, // Post-> ID;
            'numberposts' => null,
        );
    
        $attachments = get_posts($args);
    
        $post_count = count ($attachments);
    
        if ($attachments) {
            foreach ($attachments as $attachment) {
            echo "<div class=\"post photo col3\">";
                $url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet
                $img = wp_get_attachment_url($attachment->ID);
                $title = get_the_title($attachment->post_parent);//extraigo titulo
                echo '<a href="'.$url.'"><img title="'.$title.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$img.'&w=350&h=500&zc=3"></a>';
                echo "</div>";
            }
        }

    But I don't know how to finalize it - without pointing to specific post. Any ideas?:) thank u!

  2. wpismypuppet
    Member
    Posted 1 year ago #

    Looks like the code you have will work... the only problem is your reference to the 'post_parent' in your $args. Just remove that from the $args and you won't be looking for a specific post... instead you'll get the latest 25 attachments!

    Also, your $title = get_the_title($attachment->post_parent);//extraigo titulo should be changed to $title = get_the_title($attachment->ID);//extraigo titulo but that's about all that I see that's holding you back. I haven't tested it, but it should work.

    The only downfall to this is if you've upload PDF documents, or things other than images, you'll have to write a code to check for that, as this code gets attachments, not necessarily images only. But that's an easy fix. Let me know if it works out!

  3. pawciak
    Member
    Posted 1 year ago #

    thx:) the only problem is that it crashes. It behave like the script coulnd find proper url of image. take a look: LINK
    images should load below text: Ostatnio Dodane Zdjęcia

  4. wpismypuppet
    Member
    Posted 1 year ago #

    It actually doesn't crash. If you look at the source code it's pumping out images, but it seems to be filtering them through timthumb:

    <img title="img015" src="http://sp3rybnik.pl/wp-content/themes/sp3-theme/timthumb.php?src=http://sp3rybnik.pl/wp-content/uploads/2013/01/img01513.jpg&w=350&h=500&zc=3">

    If you remove the timthumb piece, the images show just fine. Are you purposely using timthumb? The code you provided above IS using it, but I assumed you intended on doing that. You can switch this line:

    echo '<a href="'.$url.'"><img title="'.$title.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$img.'&w=350&h=500&zc=3"></a>'

    to this line:

    echo '<a href="'.$url.'"><img title="'.$title.'" src="'.$img.'" /></a>' and it should work fine. If you want the images smaller, then you'll want to change:

    $img = wp_get_attachment_url($attachment->ID);

    to use wp_get_attachment_image to get the actual size you're looking for.

  5. pawciak
    Member
    Posted 1 year ago #

    eh.. I need to admit 2 things:
    1. WP Codex is a brilliant source of knowledge
    2. I'm an idiot

    ...

    This is the easiest solution, just from the codex :)

    $args = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null
      );
    
      $attachments = get_posts( $args );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<li>';
               echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
               echo '<p>';
               echo apply_filters( 'the_title', $attachment->post_title );
               echo '</p></li>';
              }
         }

    Thank u for your help @wpismypuppet :)

  6. wpismypuppet
    Member
    Posted 1 year ago #

    1. WP Codex IS brilliant... and useful.
    2. You are NOT an idiot...

    It's always good to try new things to learn more about it. And that's why this forum is so great! Lots of people here willing to help out and shed some light.

    What makes you not an idiot is the fact that you went to a suggested area and actually READ the information... something a lot of people don't do. They expect people to do the work for them. In reading, you found your solution and, best of all, you understand what it does.

    Don't be so hard on yourself... Learn from your mistakes, learn from others, and be open minded and willing. You are all those things... FAR from an idiot :)

    Please mark this post as resolved so others know you are all set. And happy coding!

  7. pawciak
    Member
    Posted 1 year ago #

    Thanks :)

    If somebody would like to use such solution - it's my code:

    <?php
    
          $args = array(
       'post_type' => 'attachment',
       'numberposts' => 12,
       'post_status' => null
      );
    
      $attachments = get_posts( $args );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<li>';
               echo wp_get_attachment_image( $attachment->ID, array('100', '100') );
               echo '</li>';
              }
         }
    
    ?>

    numberposts is showing us how much img's we will show, array('100', '100') sets the img size. Remember to put this code between

    Topic Closed

    This topic has been closed to new replies.

    About this Topic