自定义页眉链接

:discourse2: 摘要 自定义页眉链接 (Custom Header Links) 允许您轻松地向页眉添加基于文本的自定义链接。
:eyeglasses: 预览 在 Discourse 主题创建器上预览
:hammer_and_wrench: 仓库链接 GitHub - discourse/discourse-custom-header-links · GitHub
:open_book: 不熟悉 Discourse 主题? 使用 Discourse 主题的初学者指南

安装此主题组件


功能

桌面端

移动端


(由于空间非常有限,不建议在移动设备上添加多个链接)


设置

设置 描述
custom_header_links 要在页眉中显示的链接结构化列表。每个链接都通过一个带有单独字段的表单进行配置(见下文)。
links_position 控制链接是出现在页眉的右侧(默认)还是靠近徽标的左侧。当设置为 left 时,所有链接都会在主题页面上自动隐藏,为主题标题腾出空间——无论单个链接的 hide_on_scroll 设置如何。

添加链接

链接是通过主题组件设置中的结构化表单进行配置的。点击 添加 以添加新链接。每个链接都有以下字段:

字段 必需 描述
文本 (Text) :white_check_mark: 链接的可见标签。最多 100 个字符。这也决定了应用于链接的 CSS 类(见下文 CSS 自定义)。
标题 (Title) :x: 鼠标悬停在链接上时显示的工具提示文本。最多 1000 个字符。
URL :white_check_mark: 链接指向的 URL。可以是相对路径(例如 /faq)或完整 URL。最多 2048 个字符。
显示 (View) :x: 控制链接显示在哪个设备上。如果留空,链接将显示在所有设备上(与 vdm 相同)。见下文的值。
目标 (Target) :x: 控制链接的打开方式。如果留空,则默认为在新标签页中打开(与 blank 相同)。见下文的值。
滚动时隐藏 (Hide on scroll) :x: 控制在主题页面上当主题标题在页眉中可见时,链接是否隐藏。默认为 keep。仅在 links_position 设置为 right 时才适用——见下面的注释。见下文的值。
区域设置 (Locale) :x: 如果设置,仅当站点的页面语言与此值匹配时才显示链接。留空则在所有区域设置上显示链接。见下文的详细信息。

显示值 (View values):

行为
vdm 桌面和移动设备上都可见
vdo 仅在桌面端可见
vmo 仅在移动端可见
(未设置) vdm 相同——在所有设备上可见

目标值 (Target values):

行为
blank 新标签页中打开
self 同一标签页中打开
(未设置) 默认为在新标签页中打开(与 blank 相同)

滚动时隐藏值 (Hide on scroll values):

行为
keep 即使主题标题在页眉中可见,链接也保持可见 (默认)
remove 在主题标题在主题页面上可见时,链接隐藏

:information_source: hide_on_scroll 仅在 links_positionright 时适用。links_positionleft 时,无论其单独的 hide_on_scroll 设置如何,所有链接都会在主题页面上一起隐藏。

以下是 hide_on_scroll 生效的示例(当 links_position 设置为 right 时):

“点赞最多 (Most Liked)”“隐私 (Privacy)” 设置为 keep,因此当标题展开时它们仍然可见。其他链接设置为 remove,因此当标题可见时它们会隐藏。此行为仅影响主题页面。


区域设置筛选

区域设置 (Locale) 字段允许您仅在站点设置为特定语言时才显示链接。这对于希望为每种语言设置不同页眉链接的多语言社区非常有用。

  • 将字段设置为区域设置代码,例如 endefrzh_CN 等。
  • 匹配是不区分大小写的,并且 -_ 分隔符被视为相同——因此 en-USen_USen_us 都相等匹配。
  • 如果区域设置字段留空,则链接在所有区域设置上都显示。这对于大多数单语言站点是推荐的设置。
  • 链接元素上还会添加一个 CSS 类 headerLink--{locale},可用于额外的 CSS 定位。

:warning: 常见问题: 如果您的链接未出现,请检查您是否不小心设置了一个与您站点配置的语言不匹配的 locale 值。留空 locale 字段是安全的,并且将始终显示链接。


CSS 自定义

每个链接都会自动获得一个派生自其 文本 (Text) 值的 CSS 类:空格被替换为连字符,文本被转换为小写,并在末尾追加 -custom-header-links

例如:

  • 文本为 Privacy 的链接将获得类 privacy-custom-header-links
  • 文本为 Visit Shop 的链接将获得类 visit-shop-custom-header-links

样式化所有页眉链接:

.custom-header-links .headerLink a {
  font-size: var(--font-up-1);
  color: var(--header_primary);
}

样式化特定链接(例如,文本为“Privacy”的链接):

.custom-header-links .headerLink.privacy-custom-header-links a {
  color: var(--tertiary);
}
.custom-header-links .headerLink.privacy-custom-header-links a:hover {
  color: var(--tertiary-high);
}

根据登录状态显示或隐藏链接:

Discourse 会为未登录用户在 <html> 标签上添加 anon 类。您可以使用此条件性地显示或隐藏链接:

/* 隐藏“仪表板 (Dashboard)”对未登录用户的显示 */
html.anon .dashboard-custom-header-links {
  display: none;
}

/* 隐藏“注册 (Sign Up)”对已登录用户的显示 */
html:not(.anon) .sign-up-custom-header-links {
  display: none;
}

:warning: CSS display: none 是一种仅影响视觉的隐藏机制。链接的 HTML 仍然存在于页面源代码中。请勿使用此方法来保护敏感或需要访问控制的 URL。

使用 CSS 重新排序链接(使用 flexbox 的 order):

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

使用 /my 路径用于用户特定链接,以避免硬编码用户名:

/my/messages     → 当前用户的收件箱
/my/activity     → 当前用户的活动

:discourse2: 由我们托管? 主题组件可在我们的 Pro、Business 和 Enterprise 套餐中使用。


更新日志亮点:

  • custom_header_links 设置已从逗号分隔的列表格式迁移到结构化的 type: objects 表单 UI。如果您以前使用旧的逗号分隔文本输入配置了链接,迁移应该会自动保留您的数据。
83 个赞
How can I add custom hamburger menu
Header Submenus
Structuring a multilingual community
Display StatusPage status on as a header link
Control CSS based on user's logged in state
Looking for a freelancer to implement a custom Discourse theme (based on an existing mockup)
Add a global header between Discourse & Website
How to make custom setting type
Is it possible to have users' job titles next to their names on posts?
Link to find all deleted posts by all users in a particular period
Dropdown links in existing Header
Insert Link to External Website
Is anyone here using their Discourse instance as their entire website?
How to fire on every footer load (or page load?)
Custom links above header bar
Custom Header Links (icons)
Pre seeded posts all missing - Missing Terms of Service, FAQ and Privacy pages
Dropdown header links in the existing Header
Add a "curated personal learning" page for users
Redirect search icon to search page
Possible to change main logo URL?
2021: The Year in Review
How to add breadcrumb?
How to add contact us page?
Navigation bar above all post
Adding link to blog on main page
Customizing your site with existing theme components
Show "new topic" when not logged in
How can I add a new button to the topbar?
⬇️ Dropdown Header
How to fix logo size?
Links into the header block
Customize the header with links, icons, or menus
Privacy Policy does't meet Google's requirements
Custom Header Links and New Topic Header Button don't play nice
Customize Your Site Branding
Nextcloud support
How to override the site-header.hbs file from custom theme?
Sidebar link / top button to PM admin
Toolbar under site header per category
Privacy Policy does't meet Google's requirements
Link buttons on the top disappear when scrolling around the site
Disclaimer section on the "about" page
What Themes are being used for these Discourse forums?
Alternative component (plugin?) to categories navbar
Add button in header for logged in users
Use decorateWidget to add text link to header
[PAID] Expert Wanted for Forum Redesign
Add social media handles on home page
Create hyperlink from home logo
Configure a custom sidebar link to open in a new tab
2023: The Year in Review
Modifying Header
Fully Theme
Visiting /admin/upgrade may lead to a server error
Header Submenus
How do I go about making a very customized theme?
How to Integrate a Custom Plugin in discourse UI
Embedding a whole Discourse forum on another site in an <iframe>
Memberstack + Webflow + Discourse OpenID Connect
Custom website header with forum
Custom Header Links (icons)
Brave default Theme
Why does the logo navigate to Discourse home not main site home
Structuring a multilingual community
How to make sidebar sticky?
Forum navbar is repeating
Adding my Nav into the header bar
Best place(s) to link back to a non-forum homepage?
Allow non-members to pm the administrator
Adding a header link for Guests Only
Link on Top header to Go to Home page of main site
How to finely scroll out custom header?
Can I add a icon and link back to my homesite at the top bar
Custom Header links was automatically hide when scrolling topic page contents
Link behavior inconsistent
Upcoming Header Changes - Preparing Themes and Plugins
How to add a "button" which composes a pre-filled topic
Where to place project external URL?
Change destination of logo link
External header links are not opening in a new tab
How to split the forum into two main parts (general/groups) and how to display only subcategories I can create a new post in?
Add secondary url / embed into Wordpress
"An empty string is not a valid JSON string" when updating theme component #2
How can I add a button to the header?
Showing categories statically above most recent posts
How to add a button to the header area?

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 个赞