簡體   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