首页 > 解决方案 > 设置基本路径会破坏锚链接

问题描述

我创建了一个 WordPress 主题,其中的图像全部损坏,因此我在页面中添加了一个基本路径标签。

<base href="https://www.example.com/wp/wp-content/themes/my-theme/"/>

但是现在所有的锚/链接都不起作用。

<a href="#an_id_on_the_page">click here</a>

上面的链接指向“ https://www.example.com/wp/wp-content/themes/my-theme/index.php#an_id_on_the_page ”而不是同一页面,但更下方。

WordPress 建议将“”添加到每个图像的路径中。但这意味着打破工作流程并在每次更改时编辑 HTML 代码。

有什么想法可以解决这个问题吗?

更新
看起来如果我在锚前面放一个“/”,它看起来就像它正在工作。我将对其进行更多测试以确认。

标签: html

解决方案


除非明确指出,否则任何链接、命名锚点或空白 href 都不会指向原始子目录:基本标记使所有链接都不同,包括指向基本标记 url 的同一页面锚链接,例如:

<a href='#top-of-page' title='Some title'>A link to the top of the page via a named anchor</a>
becomes
<a href='http://www.example.com/other-subdirectory/#top-of-page' title='Some title'>A link to an #named-anchor on the completely different base page</a>

<a href='?update=1' title='Some title'>A link to this page</a>
becomes
<a href='http://www.example.com/other-subdirectory/?update=1' title='Some title'>A link to the base tag's page instead</a>

通过一些工作,您可以通过明确指定这些链接链接到它们所在的页面来解决您可以控制的链接上的这些问题,但是当您将第三方库添加到依赖于标准行为的组合中时,它很容易造成大混乱。

资源


推荐阅读