get_post_gallery 函数显示 url onky 不显示标题

问题描述 投票:0回答:1

我已经在编辑器上使用 WordPress 创建了一个图库,现在我正在尝试获取图像 url 和图像的标题。从下面的函数中,我只能获取 URL 和 ID,但现在可以获取标题。

<?php
  $gallery = get_post_gallery( get_the_ID(), false );
  var_dump( $gallery );
?>

这就是 var_dump 结果的显示方式

array(2) {
  ["ids"]=>
  string(19) "199,198,197,195,196"
  ["src"]=>
  array(5) {
    [0]=>
    string(83) "http://example.com/wp-content/uploads/2014/04/fff-10-150x150.png"
    [1]=>
    string(82) "http://example.com/wp-content/uploads/2014/04/fff-9-150x150.png"
    [2]=>
    string(82) "http://example.com/wp-content/uploads/2014/04/fff-8-150x150.png"
    [3]=>
    string(82) "http://example.com/wp-content/uploads/2014/04/fff-6-150x150.png"
    [4]=>
    string(82) "http://example.com/wp-content/uploads/2014/04/fff-7-150x150.png"
  }
}

我还需要做些什么才能获得图像的标题吗?

php wordpress
1个回答
2
投票
<?php  
$gallery = get_post_gallery( get_the_ID(), false );
$new1 = $gallery['ids'];
$str = $new1;
$str1 = explode(",",$str);
$i = 0;
  foreach( $gallery['src'] AS $src )
  {
    $str2 = wp_get_attachment($str1[$i]);
    $caption = $str2['caption'];
?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" /><div><?php echo $caption; ?></div>
<?php
   $i++;
 } ?> 

我想这会对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.