How to check if a post has a specific label (using conditional tags)?

拈花ヽ惹草 提交于 2020-05-26 06:42:20

问题


I'm using blogger, and I want to use different <title> tags depending on the post labels. For example, if a post has the label: "SOMELABEL1", it will show <title>Title1 - Wbsitename</title>, and if the post has the label: "SOMELABEL2", it will show <title>2222 - Webitename</title>.

I have tried to use the following code (which is working on the middle of the page - in the body), but not in the head start:

<b:if cond='data:post.labels any (l => l.name in "LABEL1")'>
<title>1111 - Webitename</title>.
<b:else/>
<title>2222 - Webitename</title>.
</b:if>

Please help.


回答1:


You can not check data:post.labels outside the Blog widget but the good news is that you can use your code inside Blog widget to change the page title via JavaScript.

<b:if cond='data:post.labels any (label => label.name == "LABEL1")'>
    <script type="text/javascript">
        document.title="1111 - Webitename";
    </script>
<b:else/>
    <script type="text/javascript">
        document.title="2222 - Webitename";
    </script>
</b:if>

But changing the page title via JavaScript won't help SEO because some web crawlers do not support JavaScript. (Google's does)



来源:https://stackoverflow.com/questions/42178327/how-to-check-if-a-post-has-a-specific-label-using-conditional-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!