简体   繁体   English

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

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

I use this code to get first image from wordpress posts in functions.php 我使用这段代码从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;
}

It gets first image from every post and I use this command in search.php of the theme template 它从每个帖子中获取第一张图片,我在主题模板的search.php中使用此命令

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

So this is used for search results: I would like that wordpress make exact dimensions of first images, is there any way, since I uploaded them manually and also use manually inserts to posts? 所以这用于搜索结果:我希望wordpress制作第一张图片的精确尺寸,有什么办法,因为我手动上传它们并使用手动插入帖子?

All I want is that wordpress makes 250px width of images that are currently 800px wide. 我想要的是wordpress使250px宽度的图像当前宽800px。

My site url is virmodrosti.com and if you search for phrase "minerali" for example you will get to this page http://www.virmodrosti.com/?s=minerali Here are all the images already resized by using width=250 but doesn't make sense, since they are still 800x450. 我的网站网址是virmodrosti.com,如果你搜索短语“minerali”,你会看到这个页面http://www.virmodrosti.com/?s=minerali这里是所有已经使用width = 250调整大小的图像但没有意义,因为它们仍然是800x450。

Thank you. 谢谢。

use below code in fucntions.php, 在fucntions.php中使用下面的代码,

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

Example : 示例:

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

REF : https://developer.wordpress.org/reference/functions/add_image_size/ 参考: https//developer.wordpress.org/reference/functions/add_image_size/
http://www.wpbeginner.com/wp-tutorials/how-to-create-additional-image-sizes-in-wordpress/ 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