$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Hardcoded Images Too Big|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Hardcoded Images Too Big

matteradmin9PV0评论

I have a ton of hardcoded images and a bunch of custom image sizes. All image URLs within the content of the posts link to the full-size image. I tried a string replace on the_content filter adding the -300x200.jpg, and this reduced my page size by half, but the load time is hellacious, so it's not worth it. Is there a better way? I looked into wp_get_attachment_image_src but can't seem to get it to do anything at all. If it helps, all the images are attached to the post. Thanks heaps!

function replace_image_url($text) {
if (is_category(‘my-cat’)){
$text = str_replace('.jpg', ‘-300x200.jpg’, $text);
return $text; 
} 
else return $text;
}
add_filter('the_content', 'replace_image_url');

The following is a much better solution, but incomplete, as there are multiple images in the content, and while it replaces the source image with the medium image (whatever size it is), it replaces all images with the first attached image. How do I get this to apply to all images with their respective medium attachment urls?

add_filter( 'the_content', 'FilterImages', 0 );
function FilterImages( $content )
{
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');  
$image = (isset($image[0])) ? $image[0] : ''; 
$content = str_replace('src=', 'src="'.$image.'"', $content);


  return $content;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far