我正在使用此代码从帖子或页面获取图像,以便在自定义滑块中使用它们。
<?php $section_content = get_page_by_path( \'partners\', OBJECT, \'post\' ); ?>
<?php $images_gallery = get_post_gallery_images( $section_content->ID ); //var_dump( $images_gallery ); ?>
<div class="swiper-container post-slider" style="height:300px;">
<div class="swiper-wrapper">
<?php if( $images_gallery ): foreach( $images_gallery as $image ): ?>
<?php //get the id of the image post.
$image_id = attachment_url_to_postid( $image );
var_dump( $image_id );
//get the image "post" information
$attached_image = get_post( $image_id );
//get the image title
$image_title = $attached_image->post_title;
//get the image caption
$image_caption = $attached_image->post_excerpt;
//var_dump($image_id, $attached_image);
?>
<div class="swiper-slide" style="background-image:url(\'<?php echo $image; ?>\');background-size:cover;">
<!-- <img class="img-fluid w-100" src="" > -->
<small class="text-white m-4 d-none d-md-block" style="position:absolute;top:0;"><?php echo $image_title; ?></small>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="swiper-scrollbar"></div>
</div>
在这里搜索之后,我找到了一个快速的解决方案,可以获取图库图像信息,如标题、描述和标题,但我对标题和id有问题。一些图像id没有返回,如果我使用var_dump()
我可以看到从函数返回的id值attachment_url_to_postid()
是int(0)
. 我发现wordpress将加载一个在url末尾附加了维度的图像。我已经检查了媒体库中的图像permalink,但url没有问题,没有附加用于显示文件的维度:http://myasite.com/wp-content/uploads/2020/03/my-image-1024x683.jpg
. 据我所知,获取id需要一个如下url:http://myasite.com/wp-content/uploads/2020/03/my-image.jpg
.<有解决方法吗?