• I want to retrive attached pictures to post, do one thing with first and something else with others. I’ve come with this code described below, but I did something funky – it’s pulling additional pictures from other post. I checked, they aren’t attached to this particular post. Also, is there a way to simplify this code?
    Here is my page

    echo '<div class="box_gallery">';
    $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
        if ($attachments) {
        foreach ( $attachments as $attachment ) {
            $show_big_pic = wp_get_attachment_image( $attachment->ID, 'medium' );
            echo '<div class="main_pic">'.$show_big_pic.'</div>';
        }
        }
    echo '<div class="box_for_small_pics">';
    $args = array( 'post_type' => 'attachment', 'offset' => 1, 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
        if ($attachments) {
        foreach ( $attachments as $attachment ) {
        $show_small_pic = wp_get_attachment_image( $attachment->ID, 'thumbnail' );
            echo '<div class="small_pics">'.$show_small_pic.'</div>';
        }
        }
    echo '</div></div>';

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter darkenpl

    (@darkenpl)

    bump

    Thread Starter darkenpl

    (@darkenpl)

    I cleaned code as I wanted, but there is a big problem with it:
    On every page it shows always the same attachments, the very first attachment I uploaded to this wordpress installation.
    Here is my page

    <?php
                echo '<div class="box_gallery">';
                $args = array( 'post_type' => 'attachment', 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID );
                $attachments = get_posts($args);
                    if ($attachments) {
                        $i = 0;
                        $len = count($attachments);
                        foreach ( $attachments as $attachment ) {
                            if ($i == 0) {
                                $picture_src = wp_get_attachment_url($attachment->ID, 'medium');
                                ?><div class="main_pic"><img src="<?php echo $picture_src; ?>"></div>
                                <div class="box_for_small_pics">
                                <?php $i++;
                            }else{
                                $picture_src = wp_get_attachment_thumb_url($attachment->ID);
                                ?><div class="small_pics"><a href="<?php echo $picture_src; ?>"><img src="<?php echo $picture_src; ?>"></a></div>
                                <?php $i++;
                            }
                        }
                    }
                echo '</div></div>';
            ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Retrive first attachment, do sth with first, do else with others’ is closed to new replies.