在ExpressionEngine中设置自定义模板变量

10

我需要在条目模板中多次输出一个条目的类别。

因此,我想要获取以下内容的输出并将其分配给一个变量,以便我可以在模板中重复使用:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}{category_name}{/categories}
{/exp:channel:entries}"

如何做到这一点?

3个回答

9
现在,您可以启用模板以允许使用PHP,然后您可以编写类似以下内容的代码:
{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}
        <?php $category = '{category_name}'; ?>
    {/categories}
{/exp:channel:entries}

然后你可以在php变量"category"中存储{category_name}。稍后您可以按照您的意愿重复使用它,例如将其打印:

<?php echo $category; ?>

您甚至可以将其与其他 EE 标签进行比较:

{exp:channel:entries channel="product" limit="1" status="open"}
    {if <?php $echo($category) ?> == title}
        This title have got the same value as the category!
    {/if}
{/exp:channel:entries}

3

Croxton的Stash:http://devot-ee.com/add-ons/stash几乎可以做到Derek(上面提到的)所做的NSM Transplant一样,并且是免费的。其中任何一个附加组件都肯定是实现您尝试完成的任务最简单的方法。


2

EE没有内置的方法来保存标签循环中的数据并在模板之外的其他地方重复使用它。

一种解决方案是使用NSM Transplant来实现您要做的事情。

另一种方法是将整个条目页面包装在channel:entries标记中,这样您就可以在需要的任何地方使用categories循环,然后对于无法嵌套在channel:entries中的任何内容,都可以使用嵌入。


谢谢,我不知道NSM Transplant - 看起来是个不错的解决方案,但对于我的情况来说成本有点过高。实际上我想使用这段代码来获取category_id并将其用于筛选一个频道条目列表(列出兄弟条目)。我将我的帖子中的代码插入到名为{my_category}的模板片段中,并将其放入了一个频道条目调用的category=""属性中,但频道条目似乎没有评估该片段。因此我打算使用嵌入式模板并传递变量。 - onblur
你应该查看相关分类模式 - Derek Hogue
http://ellislab.com/expressionengine/user-guide/modules/channel/channel_entries.html#related-categories-mode - Derek Hogue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接