主题ID作为变量?

创建自定义组件以将其附加到正确帖子会比填充模板更容易。

如果您在 admin > themes & components 下编辑主题的代码……并将其添加到 JS 选项卡,经过一些调整后,应该就能实现您想要的效果:

import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // 主题中的第一帖
    const desiredCategories = [45, 3]; // 您希望显示此内容的类别 ID 的逗号分隔列表
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- 您可以编辑下面的内容,{{this.topicId}} 将会填充主题 ID -->
      此主题的 ID 是
      {{this.topicId}}
      <!-- 您可以编辑上面的内容 -->
    {{/if}}
  </template>
}

export default apiInitializer((api) => {
  api.decorateCookedElement((element, helper) => {
    const wrapper = document.createElement("div");
    wrapper.className = "custom-topic-id"

    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});

您可以从 URL 中找到类别 ID 来配置 desiredCategories,例如,Meta 上的支持类别 ID 为 6。

您可以使用 CSS 来根据需要设置样式,例如:

.custom-topic-id {
  display: inline-block;
  background: yellow;
}

3 个赞