如何在 Drupal 6 中为多个节点引用创建逗号分隔列表?
我有一个作者 content_type,我正在将其切换为文章中的用户名。问题是一个故事可以有多个作者,而 template.php 中的以下代码只会生成第一作者。我怎样才能使其输出可读为“约翰·史密斯、简·多伊和玛丽·波平斯 | 2010 年 2 月 19 日”?
function mytheme_date($node) {
return t('By !username | !datetime',
array(
'!username' => t($node->field_author[0][view]),
'!datetime' => t($node->field_publish_date[0][view]),
));
}
请记住,我也想将其用于视图,以便节点引用也能在那里正确输出。
谢谢, 史蒂夫
I have an Author content_type that I'm switching out for username in articles. The problem is that a story can have multiple authors, and the following code in template.php will only generate the first author. How can I get this to output to be readable as "By John Smith, Jane Doe and Mary Poppins | Feb 19, 2010"?
function mytheme_date($node) {
return t('By !username | !datetime',
array(
'!username' => t($node->field_author[0][view]),
'!datetime' => t($node->field_publish_date[0][view]),
));
}
Please bear in mind that I'm also wanting to use this for views too, so that a node reference will output correctly there too.
Thanks,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您给出的示例可以输出第一作者,那么将所有作者放在逗号分隔列表中的最直接的解决方案如下:
然后您将输出
$author_list
$node->field_author[0][view]
的位置更“Drupal”的做法是将modules/cck/theme/content-field.tpl.php 复制到您的主题目录,然后将其复制一份,命名为 content-field-field_author.tpl.php。然后,您可以对新文件进行更改,这将覆盖“作者”字段的值的显示方式。然后,您可以在自定义 node-[node_type].tpl.php 文件中的任意位置输出主题 field_author 值。 (您可能需要通过管理 > 站点配置 > 性能上的按钮清除缓存数据,以便首次加载自定义模板。)
如果您的视图是“ “行样式”设置为“节点”,那么它将也使用节点和字段模板。如果将其设置为“字段”,那么您需要单独为视图中的字段设置主题。请参阅视图的“主题:信息”,了解可以在主题中覆盖的视图模板。
编辑:
错过了您想要一个自然语言列表的事实。这需要更多一点,但这里有一个 Drupalized 函数可以做到这一点:
然后,当然,使用以下代码代替上面第一个代码块的最后一行:
$author_list = implode_language($authors);
Assuming that the example you've given works to output the first author, the most direct solution to get all the authors in a comma-sperated list would be the following:
Then you'd output
$author_list
in place of$node->field_author[0][view]
A more "Drupal" way of doing it would be to copy modules/cck/theme/content-field.tpl.php to your theme directory, then make a copy of it named content-field-field_author.tpl.php as well. You can then make changes to the new file which would override how the values are displayed for the "author" field specifically. Then you could output themed field_author value wherever you want in a custom node-[node_type].tpl.php file. (You may need to clear cached data via the button on Administer > Site configuration > Performance for the custom templates to be loaded the first time.)
If your view's "Row style" is set to "Node" then it will use the node and field templates as well. If you have it set to "Fields" then you'll need to theme the field in the view separately. See your view's "Theme: Information" for views templates you can override in your theme.
Edit:
Missed the fact that you wanted a natural language list. That takes a bit more, but here's a Drupalized function that will do just that:
Then, of course, use the following in place of the last line my first code block above:
$author_list = implode_language($authors);
使用作者分类法插件怎么样?
查看多作者教程 如果您需要不同的方法。
What about using Author Taxonomy plugin?
Check Multiple authors tutorial if you need a different approach.