设置基本路径会断开锚点链接

时间:2019-03-06 19:53:24

标签: html

我已经创建了一个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代码。

是否有解决此问题的想法?

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

1 个答案:

答案 0 :(得分:1)

除非明确说明,否则任何链接,命名的锚点或空白的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>

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

Resource