如何在没有插件Timber的情况下在内容字段中继器中使用自定义字段图像裁剪尺寸



我实现了Flexible Content Field,其中一个布局包括Image。我需要裁剪它的尺寸。

我在functions.php:中添加了我的自定义尺寸

add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
add_image_size( 'custom-crop', 220, 180, true ); // (cropped)}

我的HTML代码,想在哪里调用自定义裁剪大小:

if( have_rows('top_informations') ):
while ( have_rows('top_informations') ) : the_row(); ?>
<img src="<?php the_sub_field('image')?>">

<h2><?php the_sub_field('title_text')?> </h2>

<?php endwhile;
// echo '</ul>';
endif;`

带的线表示子字段图像。我的自定义字段"图像"设置为返回URL。

首先将图像字段"Return Value"设置为"image ID",并使用以下代码。

<?php $thumb_img=wp_get_attachment_image_src(get_sub_field('image'),'custom-crop'); ?>
<img src="<?php echo $thumb_img[0]; ?>">

如果仍然不起作用,请从wpdocs_theme_setup函数中删除add_image_size,并将其直接添加到"functions.php"中。

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails', array('post') ); //add all custom post types which need featured image
add_image_size( 'custom-crop', 220, 180, true );
}

然后你需要使用我评论中的插件重新生成缩略图。

最新更新