我需要使用redux获取我在库中上传的所有图像的标题。
这是使用redux framework的主题选项中的代码:https://docs.reduxframework.com/core/fields/gallery/
我尝试使用此代码在WordPress网站上显示字幕(仅用于测试):
<?php
$attachmentIds = explode(',', $redux_demo['opt-gallery']);
foreach($attachmentIds as $attachmentId):
$metaAttachment = wp_get_attachment_metadata( $attachmentId );
echo '<pre>';
print_r( $metaAttachment );
echo '</pre>';
?>
但是,此代码返回给我[caption] =&gt; (空)
Array( [width] => 330 [height] => 180 [file] => 2015/10/330x1805.jpg [sizes] => Array ( [thumbnail] => Array ( [file] => 330x1805-150x150.jpg [width] => 150 [height] => 150 [mime-type] => image/jpeg ) [medium] => Array ( [file] => 330x1805-300x164.jpg [width] => 300 [height] => 164 [mime-type] => image/jpeg ) ) [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => [orientation] => 0 ) )
标题字段有一个值,但似乎redux不保存信息或我的代码错误?
答案 0 :(得分:0)
这是我正在寻找的解决方案:
在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
);
}
在您的模板文件中:
<?php
global $redux_demo;
$myGalleryIDs = explode(',', $redux_demo['opt-gallery']);
foreach($myGalleryIDs as $myPhotoID):
$photoArray = wp_get_attachment($myPhotoID);
?>
<a href="<?php echo wp_get_attachment_url( $myPhotoID ); ?>" class="lightbox" title="<?php echo $photoArray[caption]; ?>">
<img src="<?php echo wp_get_attachment_url( $myPhotoID ); ?>" class="img-rounded" alt="<?php echo $photoArray[title]; ?>">
</a>
<?php endforeach; ?>
我希望它有所帮助! : - )