自定义头部链接

:discourse2: Summary Custom Header Links allows you to easily add custom text-based links to the header.
:eyeglasses: Preview Preview on Discourse Theme Creator
:hammer_and_wrench: Repository Link https://github.com/discourse/discourse-custom-header-links
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

Features

Desktop

Mobile

(due to very limited space I don’t recommend adding more than one link on mobiles)


Settings

Name Description
custom header links Custom links to be displayed in the header
links position Note that when links are displayed on the left, they’re automatically hidden while scrolling within topics to make room for the title

Adding links is straightforward. Every link needs 6 items. You enter comma delimited values in this order:

link text, link title, URL, view, target, hide on scroll

Link text: the text for the link.
Link title: the text that shows when the link is hovered.
URL: The path for the link (can be relative).
View: vdm = desktop and mobile, vdo = desktop only, vmo = mobile only.
Target: blank = opens in a new tab, self = opens in the same tab.
Hide on scroll: remove = hides the link when the title is expanded on topic pages keep = keeps the link visible even when the title is visible on topic pages.

If you’re not sure what hide on scroll does, here’s an example:

Most Liked and Privacy are set to keep and so they remain visible. The other links are set to remove, and so are hidden when the title expands in the header. This only affects topic pages.

Links position: This setting allows you to change the default layout so links will appear on the left near the logo instead of on the right. Note that when positioned to the left, links will automatically be hidden when scrolling within topics to make room for the topic title.


:discourse2: Hosted by us? Theme components are available to use on our Standard, Business, and Enterprise plans.

Last edited by @JammyDodger 2024-06-11T20:31:00Z

Check documentPerform check on document:
82 个赞

don’t know if this has been suggested, tried reading back but couldn’t find it.

was thinking that maybe adding the ability to create “sub-links” thus creating the idea of a list that you could edit (text color, background hover etc.) to get results similar to what zoom has

8 个赞

the links that have the arrow to the right like developer means that it has “sub-links”

1 个赞

Is it possible to add some basic dropdown menu to an any item?

I couldn’t create a dropdown menu with “Custom header links”. It seems Zoom did that. I reviewed their dropdown menu via console, but I couldn’t figure out how they interfere with the html of this component for adding dropdown to any item.

Is there a way to add this dropdown to item? @Johani

<div id="dropdown">
  <a title="Zoom Developer Documentation" href="https://marketplace.zoom.us/docs" target="_blank">Developer</a>
  <span class="caret"></span>
  <div class="dropdown-content">
    <a title="Zoom API Docs" href="https://marketplace.zoom.us/docs/api-reference/introduction" target="_blank">API</a>
    <a title="Zoom SDK Docs" href="https://marketplace.zoom.us/docs/sdk/native-sdks/introduction" target="_blank">SDK</a>
    <a title="Zoom Developer Blog" href="https://medium.com/zoom-developer-blog" target="_blank">Blog</a>
    <a title="Zoom Developer Changelog" href="https://marketplace.zoom.us/docs/changelog" target="_blank">Changelog</a>
    <a title="Zoom Developer Survey" href="https://docs.google.com/forms/d/e/1FAIpQLSeJPLhNuxjtkxyyV276R8S_nYz99fpMbbS8VWkC8Hwi7-2Byg/viewform" target="_blank">Survey</a>
  </div>
</div>
5 个赞

Has anyone run into any issues with links not opening when setting links position to “left”?

Links work on Preview but not when I apply it to a theme.
Right alined links work fine though.

1 个赞

Hi! Thanks for creating this.
Is it possible to add an SVG before the text link? If so - how?

1 个赞

Have you found a solution for this? I am look for the same solution.

6 个赞

how can the title convert **formatting code** into actual formatting? also how about Font Awesome icons before the text?

2 个赞

Could be possible to add a flag to show or hide links as specific groups of users?

3 个赞

Is there a way this theme component can allow for dropdown navigation? That means, when I hover over a header link, it has sub-items that appear underneath it. I know that this can be done via html/css as shown here:

2 个赞

Thank you for creating this component!
One question and potentially a feature request: can we show a specific link for a specific group?
For example, if a user is in group customers I’d like to show a link to the customer’s control panel, if a user is in group workers I’d like to add a link to a different panel.
I think this would be a very useful feature.
The group name could be the last (optional) parameter.

2 个赞

太好了!我刚找到这个,之前试了很多其他的横幅和组件。这正是我需要的!

一个请求/建议:能够重新排序链接。我刚创建了一堆,意识到我想在顶部添加一个。现在我不得不手动复制粘贴所有内容,这实际上非常麻烦,因为我在向下复制时它们会不断同步/更新其他条目。

拖放或上下按钮对未来的用户(或未来的编辑)将非常有用。

4 个赞

由于我们使用了 flex 属性,您已经可以通过几行 CSS 代码轻松地操作顺序。

例如:

  • 默认顺序
    image

  • 自定义顺序
    image

CSS 示例:

.custom-header-links li {
  &:nth-child(1) {
    order: 3;
    background: red;
  }

  &:nth-child(2) {
    order: 1;
    background: green;
  }

  &:nth-child(3) {
    order: 2;
    background: yellow;
  }
}
5 个赞

@Nick_Chomey 我经常发现自己需要这样做,不仅是为了这个主题组件,还包括许多使用 type: list 设置的其他主题组件。我之前曾计划提交一个功能请求,但我完全忘记了,但既然你提醒了我,我已经在这里添加了一个:

3 个赞

@dax 谢谢!我竟然不知道这是可能的!

@keegan 看起来很棒,希望将来能被添加!

2 个赞

我也正在寻找同样的解决方案,您是否已经弄清楚他们是如何做到的了?
谢谢!

1 个赞

您好 @Johani
感谢您创建如此出色的组件!

是否可以仅在用户登录时显示链接?
这将是一个外部链接。

1 个赞

@andreas_can 欢迎来到 Meta :wave:

此组件会根据链接文本为每个链接添加一个 CSS 类。添加的类与您为链接提供的文本相同,只是空格被替换为连字符(-),并且文本设置为小写。然后,在末尾附加字符串 -custom-header-links

因此,如果您添加一个文本为

privacy

的链接,那么链接元素将具有类

privacy-custom-header-links

如果您的链接文本是

Visit Shop

那么 CSS 类将是

visit-shop-custom-header-links

所以,现在您知道了添加到每个链接的类。回到您的问题。

当用户未登录时,Discourse 会向 <HTML> 标签添加一个 CSS 类。该类是

anon

因此,您可以使用它来隐藏某些链接,使其不对未登录用户显示。假设我有一个文本为

Customer Support

的链接,并且我不想让未登录用户看到它。

然后,我将在主主题的 common > CSS 选项卡中添加此 CSS

.anon {
  .customer-support-custom-header-links {
    display: none;
  }
}

这将隐藏未登录用户的特定链接。

11 个赞

非常感谢您如此快速而详尽的回复!
这正是我想要的。
我会试试看 :slightly_smiling_face:

3 个赞

请注意,CSS display: none; 仅从浏览器渲染中隐藏链接,但对于查看页面源代码、网络爬虫和搜索引擎的用户来说,它始终是完全可见的。

3 个赞