2

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?

0

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.

  • Oh I see... I got the whole thing wrong here. I thought the add_image_size could be applied to any image in the template. – Arete May 23 '16 at 8:57
0
  1. If you wan use image from theme dir, then you should create all sizes image e.g. logo_300x300, logo_fullsize, logo_small,...
  2. You can upload image with media_handle_sideload function, more information https://codex.wordpress.org/Function_Reference/media_handle_sideload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.