# How to implement the function of logging in to view content, in order to better attract users to register

**URL:** <https://meta.discourse.org/t/how-to-implement-the-function-of-logging-in-to-view-content-in-order-to-better-attract-users-to-register/279350>\
**Category:** Support\
**Created:** [2023年九月18日 06:29 UTC](https://meta.discourse.org/t/how-to-implement-the-function-of-logging-in-to-view-content-in-order-to-better-attract-users-to-register/279350 "2023-09-18T06:29:00Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)\
**Post date:** [2023年九月18日 11:16 UTC](https://meta.discourse.org/t/how-to-implement-the-function-of-logging-in-to-view-content-in-order-to-better-attract-users-to-register/279350/8 "2023-09-18T11:16:51Z")

</div>

> [@go\_ahead](#):
>
> “Unable to view content without replying”–Active users selflessly share their knowledge and they want more responses to their posts. This is also what the forum owners want. I think this feature is beneficial to the development of the forum.

这个好办啊，topic model里面有一个attribute就显示了你是否已经回复过，叫posted，我之前写了个组件 [Composer tip under specific tag topics](https://meta.discourse.org/t/composer-tip-under-specific-tag-topics/255618/) 就判断了是否已经回复过。如果你有代码基础，可以看一下我写的。结合api的decorateCookedElement方法就能实现在没回复的情况下隐藏。

具体代码我懒得写了最近特别忙。

This is easy to do. There is an attribute in the topic model that shows whether you have replied to it. It is called `posted`. I wrote a component before ( [https://meta.discourse.org/t/composer-tip-under-](https://meta.discourse.org/t/composer-tip-under-) specific-tag-topics/255618/1 ) determines whether you have replied. If you can program, you can take a look at what I wrote. Combined with the `decorateCookedElement` method of the `API`, it can be implemented.

I am too lazy to write the specific code. I have been very busy recently.

```plaintext
<script type="text/discourse-plugin" version="1.6.0">

const user = api.getCurrentUser();

const I18n = require("I18n");

const pid = "post_hider"

const tl4_css = `
.d-editor-preview [data-theme-hide] {
  background: var(--tertiary);
  color: var(--secondary);
  border-top: 2px solid var(--secondary);
  position: sticky;
  top: 0;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.d-editor-preview [data-theme-hide]::before {
  content: "本帖子被设为隐藏";
}
`

api.addPostClassesCallback((attrs) => {
if (attrs?.cooked?.includes(`<div data-theme-hide="true">`)) {
  return ["hiddened"]; // 添加css用的，接下来css怎么写看你了
} else {
  return [];
}
});

if (!user) {
    api.decorateCookedElement((el, helper) => {
        function hide_post() {
            setTimeout( () => {
                try {
                    // document.querySelector(`[data-post-id="${helper?.widget?.attrs?.id}"]`)?.parentElement?.remove();
                } catch (err) {
                    console.log(el);
                    console.log(err);
                }
            }, 1000);
        }
        if (el?.querySelector(`[data-theme-hide="true"]`)) {
            el.innerHTML = `<p>帖子已被社区隐藏</p>`; // 修改这行
            hide_post();
        }
    }, {
        id: pid,
        afterAdopt: true,
        onlyStream: true,
    });
} 

    
</script>

```

想起来我这里有一份之前用来给未登录用户隐藏 **整个帖子** 的代码，你可以拿来参考一下

I remembered that I have a code that I used to hide **the entire post** from non-logged-in users. You can use it for reference.

---

_[View the full topic](https://meta.discourse.org/t/how-to-implement-the-function-of-logging-in-to-view-content-in-order-to-better-attract-users-to-register/279350)._
