我继承了这样的Underscore模板:
<script type="text/template" id="tpl-post">
<div class="ti-author-box">
<% _.each(authors, function(author) { %>
<% if ( author.author_image ) { %>
<img class="ti-author-image" src="<%= author.author_image %>" alt="<%= author.author_display_name %>" />
<% } else { %>
<img class="ti-author-image" src="<?php bloginfo('template_directory'); ?>/img/default-user.png?resize=40%2C40" alt="<%= author.author_display_name %>" />
<% } %>
<div class="ti-author-name" ><%= author.author_display_name %></div>
<% }); %>
</div>
...
</script>
authors
是一个作者数组,每个元素都有一个对象,其属性类似于author_image
和author_display_name
。
我的问题是,如何更改它以仅显示数组中第一位作者的信息,而不是使用_.each()遍历整个数组?
您不能像这样添加0数组索引吗?
<% if ( author[0].author_image ) { %>
<img class="ti-author-image" src="<%= author[0].author_image %>" alt="<%= author[0].author_display_name %>" />
<% } else { %>
<img class="ti-author-image" src="<?php bloginfo('template_directory'); ?>/img/default-user.png?resize=40%2C40" alt="<%= author[0].author_display_name %>" />
<% } %>
<div class="ti-author-name" ><%= author[0].author_display_name %></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句