How to insert a local image with custom size

I am not very familiar with PHP yet but I am trying to build a WordPress theme.

In one of my theme files I simply want to insert an image (located at the theme directory /img/image.jpg) with a link to http://google.com

Since I have already included add_image_size( 'testtest', 333, 333, true ); in my functions.php I would like to use this function to display the image at this size.

How should I write this with PHP code in my template file? In other words, how can I display an image that uses the pre-defined size and add a link to it with PHP?

2 Answers
2

add_image_size function has nothing to do with the images that are located in theme root directory.

When you add add_image_size function , we are saying that :

Hey WordPress! Can you please also re-size my uploaded images via WordPress admin to this particular size also hard crop the image if image size is bigger ( since third argument is set to true ).

So whenever we upload images after adding this function WordPress generate this extra size image in addition to default sizes. These generated images are available in /wp-content/uploads/ folder by default unless you have changed the default uploads folder.

So to resize the image you need to upload the image via WP admin panel and look at the uploads folder.

How should I write this with PHP code in my template file?

Generally this is used to get thumbnail attached to a post/page/CPT, like

the_post_thumbnail( 'testtest' );

The above function generates required html for image tag.

Leave a Comment