如何获取WordPress图像缩略图链接?(非特色图片)


How to get WordPress image thumbnail link? ( not featured image )

我正在尝试使用WordPress高级自定义字段和中继器字段插件构建一个简单的画廊。我需要显示一个缩略图,您单击该缩略图以放大主图像(我正在使用Fancybox)。有谁知道是否有可能获得WordPress自动生成的图像缩略图的链接?

我可以获取主图像链接:/wp-content/uploads/2014/12/slide1.jpg

但是需要获取此图像链接:/wp-content/uploads/2014/12/slide1-150x150.jpg

这是我的代码:

<?php if(get_field('gallery')): ?>
    <ul>
        <?php while(has_sub_field('gallery')): ?>
            <a class="fancybox" href="<?php the_sub_field('image'); ?>" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="<?php the_sub_field('image'); ?>" alt="" width="200px;" height="auto"/></a>
        <?php endwhile; ?>
        <?php while(has_sub_field('video_link')): ?>
            <a class="fancybox-media" href="<?php the_sub_field('video_link_snippet'); ?>"><img src="<?php the_sub_field('video_image'); ?>"/></a>
        <?php endwhile; ?>
    </ul>
<?php endif; ?>

您绝对可以使用高级自定义字段和WordPress执行此操作。为此,请进入自定义字段并更改子字段"image",使其返回图像对象而不是图像 URL。请参阅此处的"返回值"。

此字段返回对象后,请尝试var_dumping该字段以查看您的 URL 选项:

var_dump(the_sub_field('image'));

这应该显示系统中启用的不同缩略图大小,并允许您轻松获得所需的缩略图。一个可能的例子:

// Get image object with all sizes...
$image = the_sub_field('image');
$size = 'thumbnail';
// Get URL from sizes based on our $size var
$url = $image['sizes'][$size];
// Now we have the thumbnail URL
echo $url;

如果您遇到问题,请告诉我!有关更多文档和更多选项,请尝试图像字段的高级自定义字段文档

相关文章: