How to show featured image (medium sized) relative URL (relative to the website root)?

I need something like this: “uploads/214/07/image-600×400.png”

I tried with this:

if ( has_post_thumbnail()) {
$medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
echo $medium_image_url[0]; }

It works fine, I get path of medium sized featured image, but with domain name too, and I need it without domain name.

Also I tried like this:

global $wpdb;
$the_thumbnail_id = get_post_thumbnail_id($post->ID);
$the_thumbnail_name = $wpdb->get_var( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$the_thumbnail_id' AND meta_key = '_wp_attached_file'" );
echo $the_thumbnail_name;

This works fine, I get just relative path, but “full” sized featured image path, and I need “medium” sized featured image path.

Can anyone help me, maybe redesign (adding some parameter for “medium” size) of second function, or some new approach?

Read more here: How to get relative URL of medium sized featured image?


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: