Wordpress:get_post_gallery 图片自定义尺寸

Wordpress: get_post_gallery image custom size

我正在尝试手动显示 post 图库,但图像已调整为默认缩略图大小,这太小了。我正在开发一个我想在未来出售的主题,所以我想以自定义尺寸显示图库中的图像,忽略 wordpress 设置。我该怎么做?

这是我的代码:

if ( get_post_gallery() ) :
    $gallery = get_post_gallery( get_the_ID(), false );
    foreach( $gallery['src'] AS $src )
    {
        ?>
        <li data-thumb="<?php echo $src; ?>">
            <img src="<?php echo $src; ?>" alt="Gallery image" />
        </li>
        <?php
    }
endif;

我已经在我的 functions.php 文件中指定了我的首选尺寸,如下所示:add_image_size( 'slider-size', 1200, 680 );.

那么如何以特定尺寸手动显示图库图像?

在调用该画廊之前,您可以使用过滤器对 shortcode_atts_gallery 进行操作...

add_filter('shortcode_atts_gallery','force_large_images',10,3);
function force_large_images($out, $pairs, $atts) {
  $out['size'] = 'my_size'; // for example, "large" 
  return $out;
}

如果您不再需要它,也不要忘记删除该过滤器..

remove_filter('shortcode_atts_gallery','force_large_images',10,3);

另一种方法是执行您自己的查询...

    $args = array( 'post_mime_type' => 'image', 'post_type' => 'attachment', 'post_parent' => $post->ID, 'order' => 'ASC');
    $attachments = get_children($args);

    foreach( $attachments as $attachment) { 
        if ( !empty($attachment->guid ) ) {
            $img_url = wp_get_attachment_image_src($attachment->ID, 'full'); //change *full* size with your custom size

// and then do something ..
}