Wordpress裁剪图像不是缩略图而没有插件

时间:2018-01-03 14:34:00

标签: php wordpress

我正在寻找一个功能(使用PHP)在没有插件的情况下在wordpress中裁剪我的图片。我需要它用于画廊滑块而不是缩略图。

任何人都可以告诉我一个例子(例如在codepen中,例如somwhere)我怎样才能做到这一点?

这是我的index.php或home.php中的PHP代码,我想打印出来:

<?php $args = array(
    'posts_per_page' => '-1',
    'post_type' => 'home',
    'order'=> 'ASC'      
);
$the_query = new WP_Query($args); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<?php if(get_field('img1') != '') { ?>
   <div class="gallerySlider">
      <?php the_field('img1'); ?>
   </div>   
<?php } ?>


<?php if(get_field('img2') != '') { ?>
   <div class="gallerySlider">
      <?php the_field('img2'); ?>
   </div>   
<?php } ?>


<?php if(get_field('img3') != '') { ?>
   <div class="gallerySlider">
      <?php the_field('img3'); ?>
   </div>   
<?php } ?>

<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>

这是我的function.php:

if (function_exists('add_image_size')) { 
   add_image_size('biggest',800,800,false);
   add_image_size('big',600,300,array('center','center'));
   add_image_size('small',200,200,array('center','center'));
   add_image_size('smallest',100,100,array('center','center'));
}

如您所见,我使用ACF作为图像。对于每个帖子我有超过3张图片。 现在:如何在我的index / home.php中为这些图像调用此函数?

1 个答案:

答案 0 :(得分:0)

当然,这可以通过WordPress实现。 这是一个如何做到这一点的例子。

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top

此处数组表示裁剪位置,数组中的第一个值是x轴裁剪位置,第二个是y轴裁剪位置。

x_crop_position ==&gt; '左''中'或'右'。 y_crop_position ==&gt; '顶部','中心'或'底部'。

和往常一样,你可以使用

打印它们
if ( has_post_thumbnail() ) { 
    the_post_thumbnail( 'custom-size' ); 
}

有关详细信息,请参阅this

PS:这不适用于已上传的图片。