在WordPress中显示图像描述/标题

时间:2016-02-12 15:19:44

标签: php wordpress

当我尝试从wordpress中的图像中获取标题或描述时,它无效。我找不到真正的答案。我尝试了很多东西,但我不知道在哪里放一些代码。我读到我必须把它放在functions.php

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

$attachment_meta = wp_get_attachment(your_attachment_id);

这在我的循环中:

<?php echo $attachment_meta['caption']; ?>

像这样:

<section>
 <div class="container">
  <div class="row">
   <?php 
   if( class_exists('Dynamic_Featured_Image') ):
   global $dynamic_featured_image;
   global $post;
   $featured_images = $dynamic_featured_image->get_featured_images( $post->ID );
   if ( $featured_images ):
  ?>
  <?php foreach( $featured_images as $images ): ?>
  <div class="col-md-4 col-sm-4 col-xs-12">
   <img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
   <?php echo $attachment_meta['caption']; ?>
  </div>
 <?php endforeach; ?>
 <?php
 endif;
endif;
?> 
</div>
</div>
</section>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须在循环中调用wp_get_attachment()函数,如下所示:

<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
   <img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">

   $attachment_meta = wp_get_attachment($images['attachment_id']);
   <?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>