【问题标题】:Setting base path breaks anchor links设置基本路径会破坏锚链接
【发布时间】:2019-03-07 04:37:58
【问题描述】:

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

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

更新
看起来如果我在锚点前面放一个“/”,它看起来像是在工作。我会再测试一下以确认。

【问题讨论】:

  • 我认为您在此处错误地复制并粘贴了代码。请注意扩展名:image.png 而不是 index.php。使您的基本路径指向图像没有什么意义......

标签: html


【解决方案1】:

没有链接或命名锚点或空白hrefs将指向原始子目录,除非明确说明:基本标签使所有链接都不同,包括指向基本标签网址的同页锚链接,例如:

<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,

【讨论】:

  • 嗨@Dasun。这是正确的。我正在阅读,一个 / 将指向根目录。由于 WP 读入页面内容,然后将其写回,我能够在 #myanchor 之前添加 / ,这似乎可以正常工作。我已经更新了我的答案。
猜你喜欢
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2013-08-19
  • 2013-12-02
  • 2016-12-25
相关资源
最近更新 更多