captions - Get Image Description

I'm trying to make a post split in 2 columns. The first and left one is going to be any image inside the post, and

I'm trying to make a post split in 2 columns. The first and left one is going to be any image inside the post, and the second and right one will be the_content() (excluding the images).

So as of now I have no problem pulling all the images. However - I can't seem to get the image caption or title or description.

Heres my code:

<?php if ( $images = get_posts(array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby'        => 'title',
        'order'           => 'ASC',
        'post_mime_type' => 'image',
    )))
    {
        foreach( $images as $image ) {
            $attachmenturl = wp_get_attachment_url($image->ID);
            $attachmentimage = wp_get_attachment_image_src( $image->ID, full );
            $imageDescription = apply_filters( 'the_description' , $image->post_content );
            $imageTitle = apply_filters( 'the_title' , $image->post_title );
            $i++;
            if (!empty($imageTitle)) {
                echo '<div class="client-img-item ci-'.$count++.'"><img src="' . $attachmentimage[0] . '" alt="'.$imageTitle.'"  /> <div class="CAPS client-img-caption"><span class="red arrow-lrg">»</span> '.$imageDescription.'</div></div><div class="sml-dots"></div>';
} else { echo '<img src="' . $attachmentimage[0] . '" alt="" />' ; }
        }
    } else {
        echo 'No Image Found';
    }?>

I'm trying to make a post split in 2 columns. The first and left one is going to be any image inside the post, and the second and right one will be the_content() (excluding the images).

So as of now I have no problem pulling all the images. However - I can't seem to get the image caption or title or description.

Heres my code:

<?php if ( $images = get_posts(array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby'        => 'title',
        'order'           => 'ASC',
        'post_mime_type' => 'image',
    )))
    {
        foreach( $images as $image ) {
            $attachmenturl = wp_get_attachment_url($image->ID);
            $attachmentimage = wp_get_attachment_image_src( $image->ID, full );
            $imageDescription = apply_filters( 'the_description' , $image->post_content );
            $imageTitle = apply_filters( 'the_title' , $image->post_title );
            $i++;
            if (!empty($imageTitle)) {
                echo '<div class="client-img-item ci-'.$count++.'"><img src="' . $attachmentimage[0] . '" alt="'.$imageTitle.'"  /> <div class="CAPS client-img-caption"><span class="red arrow-lrg">»</span> '.$imageDescription.'</div></div><div class="sml-dots"></div>';
} else { echo '<img src="' . $attachmentimage[0] . '" alt="" />' ; }
        }
    } else {
        echo 'No Image Found';
    }?>
Share Improve this question asked Dec 6, 2013 at 4:29 MoishyMoishy 3241 gold badge3 silver badges10 bronze badges 1
  • 2 As of WordPress 3.5.0 wp_prepare_attachment_for_js( $attachment ) will do the trick :) – Sven Commented Mar 26, 2018 at 8:59
Add a comment  | 

1 Answer 1

Reset to default 28
function wp_get_attachment( $attachment_id ) {

$attachment = get_post( $attachment_id );
return array(
    'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
    'caption' => $attachment->post_excerpt,
    'description' => $attachment->post_content,
    'href' => get_permalink( $attachment->ID ),
    'src' => $attachment->guid,
    'title' => $attachment->post_title
);
}

Source

As sporkme explains later in the thread, this is dumped into your functions.php and can then be called with $attachment_meta = wp_get_attachment(your_attachment_id);.

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

相关推荐

  • captions - Get Image Description

    I'm trying to make a post split in 2 columns. The first and left one is going to be any image inside the post, and

    10天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信