2
votes

I am trying to load images through custom fields, rather than using the featured image.

My custom fields are as follows

name:  image-small
value: /images/thumbs/small/example.jpg

PHP

<img src='<?php echo get_post_meta($post->ID, 'image-small', true); ?>'>

yet instead of writing the img src as

http://localhost/project.co.uk/images/thumbs/small/example.jpg

it displays

http://localhost/images/thumbs/small/example.jpg

is there a way to either write the htaccess so that it re-routes the url or, do I have to hardcode it into the HTML until I go live, then replace the code with whats above? Or is there a much easier way to do it?

I have found

but it didn't help me resolve it.

1
Perhaps: <img src='<?php echo get_post_meta($post->ID, $_SERVER['SERVER_NAME'] . 'image-small', true); ?>'>? - David Thomas
i will try that. thanks Dave/David :) - m33bo

1 Answers

1
votes

Your problem is that the URL in that field begins with a "/", which means "go back to the root of the domain". Since as far as your browser is concerned the domain is "localhost", you get the "http://localhost/images/..." resolution.

Note that it's your browser doing this interpretation, not the PHP code.

What you need to do is echo at the beginning of the src="" attribute the base that you want to put on the URL, so that you're outputting the full URL from "http://" onwards. Looking at your example, this will probably be home_url() or site_url()

That is, your code will become something like: <img src='<?php echo home_url() . get_post_meta($post->ID, 'image-small', true); ?>'>