首页 文章

Wordpress:get_post_gallery图像自定义大小

提问于
浏览
0

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

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

相关问题