-1

I am trying to display the images attached to a post in single.php I've used :

<?php if (have_posts()) : while(have_posts()) : the_post(); ?>

$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID
);


$attachments = get_posts($args);

if ($attachments) : ?>
    <ul class="portfolio-image-list">

    <?php foreach($attachments as $attachment): ?>
        <li class="box">
        <figure>
        <?php the_attachment_link($attachment->ID, true); ?>
            <figcaption><?php echo $attachment->post_title; ?></figcaption>
        </figure>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php else : ?>

            <p>No images found for this post.</p>

<?php endif; ?>

 <?php endwhile; ?>
 <?php endif; ?>

However, the image seems to be in the post content (as confirmed by var_dump($post) and there are no attachments var_dump($attachments) gives me an empty array.

I seem to be misunderstanding something here...

var_dump($post); gives me :

 object(WP_Post)#136 (25) { 
 ["ID"]=> int(91) 
 ["post_author"]=> string(1) "1" 
 ["post_date"]=> string(19) "2013-09-04 13:14:17" 
 ["post_date_gmt"]=> string(19) "2013-09-04 13:14:17" 
 ["post_content"]=> string(285) "Snowy Egret"
 ["post_title"]=> string(11) "Snowy Egret"
 ["post_excerpt"]=> string(0) ""
 ["post_status"]=> string(7) "publish"
 ["comment_status"]=> string(4) "open"
 ["ping_status"]=> string(4) "open"
 ["post_password"]=> string(0) ""
 ["post_name"]=> string(11) "snowy-egret"
 ["to_ping"]=> string(0) ""
 ["pinged"]=> string(0) ""
 ["post_modified"]=> string(19) "2013-09-06 08:36:01"
 ["post_modified_gmt"]=> string(19) "2013-09-06 08:36:01      
 ["post_content_filtered"]=> string(0) ""
 ["post_parent"]=> int(0)
 ["guid"]=> string(44) "/trouble_focusing/?p=91" 
 ["menu_order"]=> int(0)
 ["post_type"]=> string(4) "post"
 ["post_mime_type"]=> string(0) ""
 ["comment_count"]=> string(1) "0"
 ["filter"]=> string(3) "raw"
 ["format_content"]=> NULL
}
10
  • Try using posts_per_page in place of numberposts. For more information visit this wordpress.stackexchange.com/questions/28929/… Commented Sep 6, 2013 at 8:34
  • tried it. No luck :( Interesting though...
    – mantis
    Commented Sep 6, 2013 at 8:37
  • Try removing <?php if (have_posts()) : while(have_posts()) : the_post(); ?> <?php endwhile; ?> <?php endif; ?> this code. Commented Sep 6, 2013 at 8:48
  • tried that too. Funny, its exactly the same.
    – mantis
    Commented Sep 6, 2013 at 8:52
  • Can you show me output of var_dump($post) ? Commented Sep 6, 2013 at 9:00

1 Answer 1

0

Try query post instead of $attachments = get_posts($args); or do it like

<?php
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; 
wp_reset_postdata();?>

also check this url http://codex.wordpress.org/Template_Tags/get_posts

6
  • I think you might be missing some code there.
    – mantis
    Commented Sep 6, 2013 at 8:41
  • also try "posts_per_page "
    – hariprasad
    Commented Sep 6, 2013 at 8:41
  • No, that gets me a list of attachments. I want only those connected with the single post.
    – mantis
    Commented Sep 6, 2013 at 8:45
  • 1
    wordpress.stackexchange.com/questions/39241/…
    – hariprasad
    Commented Sep 6, 2013 at 8:48
  • is that code working?
    – hariprasad
    Commented Sep 6, 2013 at 9:46

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.