繁体   English   中英

如何为wordpress制作自定义图像调整大小命令?

[英]How to make custom image resize command for wordpress?

我使用这段代码从functions.php中的wordpress帖子中获取第一张图片

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

它从每个帖子中获取第一张图片,我在主题模板的search.php中使用此命令

<div class="imgdiv"><a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image() ?>" width="250"></a></div>

所以这用于搜索结果:我希望wordpress制作第一张图片的精确尺寸,有什么办法,因为我手动上传它们并使用手动插入帖子?

我想要的是wordpress使250px宽度的图像当前宽800px。

我的网站网址是virmodrosti.com,如果你搜索短语“minerali”,你会看到这个页面http://www.virmodrosti.com/?s=minerali这里是所有已经使用width = 250调整大小的图像但没有意义,因为它们仍然是800x450。

谢谢。

在fucntions.php中使用下面的代码,

add_image_size( string $name, int $width, int $height, bool|array $crop = false )

示例:

add_image_size( 'singlepost-thumb', 590, 999 ); 

参考: https//developer.wordpress.org/reference/functions/add_image_size/
http://www.wpbeginner.com/wp-tutorials/how-to-create-additional-image-sizes-in-wordpress/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM