Hexo 模板函数方法:指定内容或模块只在首页显示

案例一:只在首页显示友情链接

通过 is_home 实现

<% if (is_home()){ %>
    <%= __('友情链接') %> <a href="http://yijile.com/" target="_blank" rel="nofollow">Yijile</a>
<% } %>

关键代码

<% if (is_home()){ %>
    <!-- 这里写要在首页显示的内容 -->
<% } %>

案例二:文章标题在首页设置链接,在文章页或其他页面不设置链接直接显示文本

通过 index 实现

  <% if (index){ %>
    <h1 itemprop="name">
      <a class="<%= class_name %>" href="<%- url_for(post.path) %>"><%= post.title %></a>
    </h1>
  <% } else { %>
    <h1 class="<%= class_name %>" itemprop="headline name">
      <%= post.title %>
    </h1>
  <% } %>

关键代码

<% if (index){ %>
    <!-- 这里写要在首页显示的内容 -->
<% } %>

is_home 和 index 方法对比

我使用的时候发现 index 的方法有时候会有问题。再尝试 is_home 就解决了问题。山高树茂,方法总是比问题多,多试试总能搞定问题。上次使用 index 具体的问题已经忘记了,整体来讲使用 is_home 感觉会更规范。然后如果只是不再首页显示,使用 !index s是非常方便。

联合发布:https://1px.run/hexo/

Relay Tips: 一极乐https://yijile.com/log/600/