Jiban
Jiban

Reputation: 51

WP image link changes on permalink change

Migrated my joomla site to WP. The images were in a folder named 'asets' in joomla site. I copied the dir to wp and images work fine while url structure is raw. But if I change the url permalink, the link to images also change.

like
sitename/?p=123 image path = sitename/asets/imagefile this works.

sitename/samplepost image path = sitename/post-name/asets/imagefile image not found

sitename/archive/123 image path = sitename/archive/asets/imagefile image not found

Please help me to solve this problem.

Upvotes: 0

Views: 162

Answers (1)

Moshe Harush
Moshe Harush

Reputation: 701

Just use absloute path or relative path with slash at start of path to get the root path.

I guss your HTML code look now like this:

<img src="assets/imagefile.jpg" alt="" />

So is it a relative path, the browser add the src to current url. But if you change this to relative path from root it's will be work:

<img src="/assets/imagefile.jpg" alt="" />

Or using full absolute path:

<img src="http://example.com/assets/imagefile.jpg" alt="" />

All about HTML file paths

More important thing, in wordpress you must to work only in theme directory scope And use get_stylesheet_directory_uri() function.

Your code need to look like this:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/imagefile.jpg" alt="" />

Upvotes: 1

Related Questions