Php 在WordPress中显示图像描述/标题
当我试图从wordpress中的图像中获取标题或描述时,它不起作用。我找不到真正的答案。我尝试了很多东西,但我不知道我必须把一些代码放在哪里。我读到我必须把它放到Php 在WordPress中显示图像描述/标题,php,wordpress,Php,Wordpress,当我试图从wordpress中的图像中获取标题或描述时,它不起作用。我找不到真正的答案。我尝试了很多东西,但我不知道我必须把一些代码放在哪里。我读到我必须把它放到functions.php: function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_w
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>
“alt=”“class=“img响应式”>
我做错了什么?您必须在循环中调用
wp\u get\u 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; ?>
“alt=”“class=“img响应式”>
$attachment\u meta=wp\u get\u attachment($images['attachment\u id']);
必须在循环中调用wp\u get\u 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; ?>
“alt=”“class=“img响应式”>
$attachment\u meta=wp\u get\u attachment($images['attachment\u id']);