• hello,

    How can I grab an images Caption and display it in a custom page template? Separately and independently from the image itself.

    Currently displaying caption text using template on a particular WordPress Page with a custom field. But if I can just get it directly I won’t even have to create the custom field and re-enter the caption text from the images attachment metadata.

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • $attachment = get_post('165'); /* your ID */
    $caption = $attachment->post_excerpt;
    echo $caption;
    Thread Starter ratamatcat

    (@ratamatcat)

    thanks gents,

    I should have earlier been clearer and said this ‘Caption’ is the metadata which user enters for the image when uploaded into WordPress media library, i.e. Title, Caption, Alt text, and Description. I am sure you are familiar.

    after some more study it now must be the Description instead of Caption. Suitable for longer text.

    Thanks for the code provided but I am having trouble seeing how to target each WordPress Page with the php code. So would a revised simple method work..

    rather than using php in page template, what if I use per-Page approach instead.

    So on a WordPress Page I insert an image using visual editor

    then below it in editor place a shortcode to pull and place it’s Description right underneath image. (Just like the default WordPress setup of Caption is shown underneath a thumbnail image but I am going to be inserting a medium or Large image size and require the Description instead of Caption). Normally Description is on Attachment page on link off

    If shortcode has no chance then I wonder if php could be used so the ID doesn’t need to be mentioned but the code somehow just automatically loads the Description for each image for each page. Put some code in functions then also in custom template. The is_page thing is very repetitive

    thanks

    Thread Starter ratamatcat

    (@ratamatcat)

    Actually this is exactly what the Attachment Page does (displays the Description with the Medium image size) so I need to figure out how to get this behavior but without linking off to the attachment page

    Thread Starter ratamatcat

    (@ratamatcat)

    Having dug depper I find the Attachment Page is exactly what I need. Now I won’t have to create hundreds of WordPress pages to hook into my custom template I’ll just use the Attachment Page WordPress already creates for each image which is uploaded and the existing theme template it uses, for further customization. And because its a WordPress thing there are online tutorials, for example adding new custom fields to the image metadata and showing these in the template. This will save me heaps of time. Thank god for the effort of my trying research over the past week and also your advice.

    The people behind WordPress really know what they are doing!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get image caption use in page template’ is closed to new replies.