3

I want to have the ability to use pictures from the media library in a jquery slider on the home page so that it's easy for someone else to update the pictures without having to hardcode it. I have attached a bunch of photos to a post and tried this

<?php
$image_query = new WP_Query(array('name'=>'slider-images'));
while ( $image_query->have_posts() ) : $image_query->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 '<img src="'.wp_get_attachment_url($attachment->ID).'" />';
            echo '</li>';
        }
    }
endwhile;
wp_reset_postdata();
?>

but it doesn't display anything. Is there something wrong with my code or is there an easier/better way to group images together rather than put them in a post?

EDIT: If I use the_content() in my $image_query loop it outputs the images like

<p>
    <a href="...">
        <img src="..." />
    </a>
</p>

but what I need is something like

<li>
    <a href="...">
        <img src="..." />
    </a>
</li>
4
  • What does the source code show for wp_get_attachment_url
    – Wyck
    Commented Aug 9, 2012 at 20:08
  • @Wyck the source code doesn't show the li or img tags. If I var_dump($attachments) it's an array with 0 elements Commented Aug 9, 2012 at 20:17
  • Did you actually upload images to post with an id of 36?
    – Wyck
    Commented Aug 9, 2012 at 20:31
  • yes I did. I have edited the query to use the slug name of the post instead. I know the query works because the_content() outputs the images. Commented Aug 10, 2012 at 0:57

1 Answer 1

5

It's better to use get_children than get_posts. Here is a quick example that will work. This is in the form of a function that is defined in your plugin or in functions.php then use the function as a template tag.

    /**
     * Gets all images attached to a post
     * @return string
     */
    function wpse_get_images() {
        global $post;
        $id = intval( $post->ID );
        $size = 'medium';
        $attachments = get_children( array(
                'post_parent' => $id,
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'order' => 'ASC',
                'orderby' => 'menu_order'
            ) );
        if ( empty( $attachments ) )
                    return '';

        $output = "\n";
    /**
     * Loop through each attachment
     */
    foreach ( $attachments as $id  => $attachment ) :

        $title = esc_html( $attachment->post_title, 1 );
        $img = wp_get_attachment_image_src( $id, $size );

        $output .= '<a class="selector thumb" href="' . esc_url( wp_get_attachment_url( $id ) ) . '" title="' . esc_attr( $title ) . '">';
        $output .= '<img class="aligncenter" src="' . esc_url( $img[0] ) . '" alt="' . esc_attr( $title ) . '" title="' . esc_attr( $title ) . '" />';
        $output .= '</a>';

    endforeach;

        return $output;
    }
2
  • do I call this function in my $image_query loop? I tried that but it just outputs an empty string Commented Aug 9, 2012 at 20:13
  • If it outputs an empty string then no attachments are being found. Its meant to be called in within any loop that is returning the $post.
    – Chris_O
    Commented Aug 9, 2012 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.