WordPress.org

Ready to get started?Download WordPress

Forums

retrieve first image's caption ... in excerpt (5 posts)

  1. Dunkkan
    Member
    Posted 4 years ago #

    Hello,

    I'd like to know how to retrieve the first image's caption in a post's Loop, next to the excerpt for example.

    I found a function called wp_get_attachment_metadata but it doesn't seems to do the trick.

    Any tip is appreciated,

  2. Dunkkan
    Member
    Posted 4 years ago #

    Here's some code I'm working with :

    function retrievecaption() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
      if($files) :
      //I'm lost here
    }

    I'm lost there.

  3. Dunkkan
    Member
    Posted 4 years ago #

    I got it working, except that only for the title (not the caption), and from those images uploaded (not just linked) :

    Inside the Loop :

    <?php $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => 1,
    	'post_parent' => $post->ID
    	);
    $attachments = get_children($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    	}
    }
    ?>

    Taken from the Codex.

    I hope someone can add/substract a bit more of code there :P

  4. mexsillus
    Member
    Posted 4 years ago #

    I am looking for the exact same thing. I googled the ba'jesus out of this topic and couldn't find how to retrieve the caption.

    It seems like there are lots of issues for people trying to format the captions, which wouldn't really exist if you could call the caption and get plain text back.

  5. Dunkkan
    Member
    Posted 4 years ago #

    Two more essays (with no succes) :

    <?php
    if($images =& get_children( 'post_type=attachment&post_parent='. $post->ID)){
       foreach($images as $image){
          $caption = $image->post_excerpt;
          $description = $image->post_content;
          echo $caption;
          echo $description;
       }
    }
    ?>
    <?php
    if($images =& get_children( 'post_type=attachment&post_parent='. $post->ID)){
       foreach($images as $image){
    echo apply_filters('the_excerpt', $image->post_excerpt);
    echo apply_filters('the_content', $image->post_content);
       }
    }
    ?>

    I really don't know why I can't get things printed.
    Even if the image is in the uploads folder.

    At least I get no errors :)

    As a radical approach (and don't know if has something to do indeed), we could use the wpdb Class, which theoretically can acces to anything grabbed in the database ?

    Which I don't know, is how do we write the template tag for that.

    How do I call the caption, how do I relate it to the current post, etc.

    So please it would be really cool to pull captions and descriptions dynamically, in a custom way.

Topic Closed

This topic has been closed to new replies.

About this Topic