theme development - Loop through all posts, show attachment if there

I've created two simple foreach loops: one for all posts, one for all attachments. I want it show every post title,

I've created two simple foreach loops: one for all posts, one for all attachments. I want it show every post title, and if there is an attachment, show that attachment.

I have, thus far:

$get_posts_array = array( 'posts_per_page' => 3, 'post_type' => 'post' );
$get_posts = get_posts( $get_posts_array );

foreach ($get_posts as $post)
{
    the_title();

    $get_images_array = array( 'posts_per_page' => 1, 'post_type' => 'attachment' );
    $get_images = get_posts($get_images_array);

    if ($get_images)
    {
        foreach ( $get_images as $post )
        {
        ?>
            <li> <?php the_attachment_link( $post->ID ) ;?> </li>
        <?php
        } 
    }
}
?>

However, it is not working as expected.

It retrieves every post title, but uses the same first attachment for all posts.

Any help would be great (I'm inexperienced with PHP, so this could be completely buggy).

I've created two simple foreach loops: one for all posts, one for all attachments. I want it show every post title, and if there is an attachment, show that attachment.

I have, thus far:

$get_posts_array = array( 'posts_per_page' => 3, 'post_type' => 'post' );
$get_posts = get_posts( $get_posts_array );

foreach ($get_posts as $post)
{
    the_title();

    $get_images_array = array( 'posts_per_page' => 1, 'post_type' => 'attachment' );
    $get_images = get_posts($get_images_array);

    if ($get_images)
    {
        foreach ( $get_images as $post )
        {
        ?>
            <li> <?php the_attachment_link( $post->ID ) ;?> </li>
        <?php
        } 
    }
}
?>

However, it is not working as expected.

It retrieves every post title, but uses the same first attachment for all posts.

Any help would be great (I'm inexperienced with PHP, so this could be completely buggy).

Share Improve this question asked Feb 27, 2014 at 17:36 tmyietmyie 8732 gold badges13 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can't use the_title() as you haven't set up the post data.

Instead for the title you need to use:

$post->post_title;

To get the attachments you can then use get_children() something like this:

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

$attachments = get_children( $args );

foreach($attachments as $attachment) {
  // Do your stuff
}

The thing is there might be multiple attachments on a post, so it might not be predictable. Also what kind of attachment do you want to get? You can use:

'post_mime_type' => 'image'

for example, as part of the argument - if you only want images.

What might be better, if you have the option, is to use the post thumbnail or a meta key/value (custom field) instead so the user can specifically add an attachment.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745584715a4634464.html

相关推荐

  • theme development - Loop through all posts, show attachment if there

    I've created two simple foreach loops: one for all posts, one for all attachments. I want it show every post title,

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信