Wordpress:get_post_gallery图像自定义大小

时间:2015-03-27 21:22:02

标签: wordpress post gallery

我试图手动显示帖子库,但是图片会调整为默认缩略图大小,这太小了。我正在开发一个我希望将来销售的主题,所以我想以自定义尺寸显示图库中的图像,忽略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 );

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

1 个答案:

答案 0 :(得分:0)

在调用该图库之前,您可以使用过滤器对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 ..
}