5

WordPressで画像の添付URLを取得するのに少し問題があります。これはこれまでの私のコードです:

<?php // find images attached to this post / page.
            global $post;
            $thumb_ID = get_post_thumbnail_id( $post->ID );
            $args = array(
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'numberposts' => -1,
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'exclude' => $thumb_ID,
                'post_parent' => $post->ID
            );
            $images = get_posts($args);
            ?>

            <?php // Loop through the images and show them

            if($images)
            {
            foreach($images as $image)  
            {

            echo  wp_get_attachment_image_url($image->ID, $size='attached-image');

            }
            }

?>

これは何も返しません。これと交換するwp_get_attachment_image_url($image->ID, $size='attached-image');wp_get_attachment_image($image->ID, $size='attached-image');問題なく動作しますが、URLだけでなく画像も表示されます。

4

1 に答える 1

7

次のコードは、すべての画像の添付ファイルをループして、srcurlを出力します。必要に応じて、2つの方法が示されていることに注意してください。

<?php

    global $post;
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);
    if ($attachments) {
            foreach ( $attachments as $attachment ) {
                    // Method #1: Allows you to define the image size
                    $src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
                    if ($src) {echo $src[0];}
                    // Method #2: Would always return the "attached-image" size
                    echo $attachment->guid;
            }
    }

?>
于 2012-04-30T20:05:25.117 に答える