PHP WordPress

How to get featured image url in wordpress.

to get featured image url in wordpress use following code

<?php
$featured_img_url = wp_get_attachment_url(get_post_thumbnail_id($wp_query->ID));
echo esc_url($featured_img_url); 

if you are not using custom query loop then simply use

<?php
$featured_img_url = wp_get_attachment_url(get_post_thumbnail_id());
echo esc_url($featured_img_url); 

Also you can use this

<?php
echo get_the_post_thumbnail_url();

get featured image size

you can pass the featured image sizes

<?php
echo get_the_post_thumbnail_url("full");
echo get_the_post_thumbnail_url("medium");
echo get_the_post_thumbnail_url("large");
echo get_the_post_thumbnail_url("medium_large");
echo get_the_post_thumbnail_url("thumbnail");

If get_the_post_thumbnail_url not working then check add_theme_support( 'post-thumbnails' ); is in your functions.php or not.

you can set these sizes by going to settings->media and change the default sizes value.

How to crop images in WordPress via code ?

you can define your own custom size also.

in functions.php

<?php
add_image_size( 'gallery-image-size', 300, 300, true );

now you can also use the size gallery-image-size.

Function Reference :

Leave a comment