将我的joomla网站迁移到WP。这些图像位于joomla网站中名为"asets"的文件夹中。我将目录复制到 wp,图像工作正常,而 url 结构是原始的。但是,如果我更改网址永久链接,则指向图像的链接也会更改。
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.
只需在路径开头使用绝对路径或带斜杠的相对路径即可获取根路径。
我让你的 HTML 代码现在看起来像这样:
<img src="assets/imagefile.jpg" alt="" />
所以它是一个相对路径,浏览器将src添加到当前url。但是,如果您将其更改为从根目录开始的相对路径,它将起作用:
<img src="/assets/imagefile.jpg" alt="" />
或使用完整的绝对路径:
<img src="http://example.com/assets/imagefile.jpg" alt="" />
所有关于 HTML 文件路径
更重要的是,在wordpress中,您必须仅在主题目录范围内工作并使用get_stylesheet_directory_uri((函数。
您的代码需要如下所示:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/imagefile.jpg" alt="" />