• Resolved justwander

    (@justwander)


    Hello,
    I am looking to add the image caption text to the attachment page on my site. At present I get only the image description.

    I found the following function in a post on the www.remarpro.com site:

    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
    	);
    }

    Call the function with:
    $attachment_meta = wp_get_attachment(your_attachment_id);

    Then put the text on the webpage with:
    echo $attachment_meta['caption'];

    Beginner that I am I understand most of what is here except this, how/where do I get the ID number?

    When I look at the function call I assume that the “attachment” is not the page I am building but the image I am putting on the page. Since the image appears on the page OK the ID was used, right? Where do I get it to include in the $attachment_meta call?

    ps
    If you go to the URL included with this question just click on an image to go to the attachment page for the image.

    • This topic was modified 7 years ago by justwander.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • David Sword

    (@davidsword)

    When on attachment.php the attachment (the media item) and the actual attachment page are the same. So the $attachment_id can be found with get_the_ID().

    I put the following in my themes and it gave me the caption as well as description.

    declared the custom function in functions.php (as it’s not part of WordPress, just someones idea!)

    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
      );
    }

    in attachment.php I then call for the extra data within the loop, and show what it contains for confirmation it works

    $attachment_meta = wp_get_attachment(get_the_ID());
    
    echo "<pre>";
    print_r(?$attachment_meta);
    echo "</pre>";

    and more specifically, gather what’s needed

    echo "Caption: {$attachment_meta['caption']}<br />";
    echo "Description: {$attachment_meta['description']}<br />";
    Thread Starter justwander

    (@justwander)

    @davidsword, it works perfectly.

    Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using the post ID’ is closed to new replies.