0

I'm trying to automate some listings and in doing so need to run some PHP, the PHP is used to detect which category is the same as as the current page's url title then outputs the ID of the category with the same url title.

I then want to apply this ID to a list of products so that it outputs the relevent items (like categories but giving the flexibility of using Structure).

However I'm guessing because i'm defining the variable on the same page it isn't working for some reason, any ideas? My PHP parsing in template is set to "Output".

         {exp:channel:entries}
           {categories}{if category_url_title == "{last_segment}"}<?php $category = '{category_id}'; ?>{/if}{/categories}
        {/exp:channel:entries}

        {exp:channel:entries category="<?php echo $category; ?>" dynamic="no" channel="products"}
          <h2>{title}</h2>
        {/exp:channel:entries}
4

1 回答 1

2

不要打扰!把事情简单化:

Low 的 Seg2Cat

使用{last_segment_category_id}替换上面的所有代码:

{exp:channel:entries category="{last_segment_category_id}" dynamic="no" channel="products"}
    <h2>{title}</h2>
{/exp:channel:entries}

您的代码永远无法工作的原因是因为您希望第一部分首先运行 EE,然后 PHP 保存变量,然后您希望 PHP 继续并设置第二部分,然后您希望 EE 启动再次使用 PHP 变量。一个接着一个,恐怕不是在两个之间跳跃。

解决此问题的另一种方法是使用嵌入:

第一个模板:

{exp:channel:entries}
    {categories}{if category_url_title == "{last_segment}"}{embed="my-templates/2nd-template" category="{category_id}"}{/if}{/categories}
{/exp:channel:entries}

第二个模板:

{exp:channel:entries category="{embed:category}" dynamic="no" channel="products"}
    <h2>{title}</h2>
{/exp:channel:entries}
于 2013-08-16T18:47:52.913 回答