在登录用户的头部添加按钮

如何为仅登录用户在标题中添加按钮?我需要为该按钮添加自定义文本和链接。

您是否已搜索 Theme component 类别,看看是否已存在符合您需求的内容?

1 个赞

仅向登录用户显示

您可以定位匿名用户以隐藏元素。例如,这会从未登录用户那里隐藏导航侧边栏菜单中的标签。您可以使用类似的 CSS 代码从匿名用户那里隐藏标题中的任何元素。

.anon .sidebar-section-wrapper.sidebar-section[data-section-name="tags"] {
    display: none;
}
2 个赞

这是一个很棒的解决方案,
我其实只想在顶部放一个按钮。
我最终的做法是问 ChatGPT 如何手动添加代码,它给了我一个可行的解决方案……

Header:

<script type="text/discourse-plugin" version="0.8.22">
api.decorateWidget('header-buttons:after', helper => {
    let currentUser = api.getCurrentUser();
    if (!currentUser) return; 
    
    return helper.h('li.custom-login-button-li', [
        helper.h('a.custom-login-button', { href: 'ENTER URL HERE', target: '_blank'}, 'Button')
    ]);
});
</script>

CSS

.custom-login-button {
    display: inline-block;
    margin: 0;
    padding: 0.5em 1em;
    text-align: center;
    vertical-align: middle;
    border: 1px solid transparent;
    font-weight: bold;
    line-height: 1.5;
    border-radius: 3px;
    color: #fff !important;
    background-color: #0084ff;
    border-color: #0076e6;
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out;
}

.custom-login-button:hover {
    background-color: #0076e6;
    border-color: #0065d0;
}

.custom-login-button-li {
    list-style-type: none;
}
1 个赞

干得好@Sami_Syed。

我在论坛上为仅限移动用户使用一个图标而使用了上面的图标链接TC。

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.