简体   繁体   中英

How to make custom image resize command for wordpress?

I use this code to get first image from wordpress posts in functions.php

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

<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?

All I want is that wordpress makes 250px width of images that are currently 800px wide.

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.

Thank you.

use below code in 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/
http://www.wpbeginner.com/wp-tutorials/how-to-create-additional-image-sizes-in-wordpress/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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