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

onb*_*lur 10 expressionengine

我需要在条目模板中输入几次条目.

所以我想从下面得到输出并分配给一个变量,这样我就可以在模板中重用:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}{category_name}{/categories}
{/exp:channel:entries}"
Run Code Online (Sandbox Code Playgroud)

怎么做?

Mar*_*son 9

现在,您可以启用模板以允许PHP,然后您可以编写如下内容:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}
        <?php $category = '{category_name}'; ?>
    {/categories}
{/exp:channel:entries}
Run Code Online (Sandbox Code Playgroud)

然后你有{category_name}存储在php变量"类别".稍后您可以根据需要重复使用它,就像回应一样:

<?php echo $category; ?>
Run Code Online (Sandbox Code Playgroud)

您甚至可以将其与其他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}
Run Code Online (Sandbox Code Playgroud)


Der*_*gue 2

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

一种解决方案是使用NSM Transplant来完成您想要做的事情。

另一种方法是将整个入口页面包装在标签中channel:entries,这样您就可以categories在需要的地方使用循环,然后对无法嵌套在channel:entries.