次のように出力を生成できます。
Cat1
SubCat1
SubCat3
ただし、出力が必要です
Cat1
Subcat1
SubCat2
SubCat3
メニューを作成するために、db 構造を次のように使用します。
site_id | parent_id | site_name | site_url | Level
1 | 0 | Category 1 |example.com| 0
2 | 1 | Sub Cat 1 |example.com| 1
3 | 2 | Sub Cat 2 |example.com| 2
4 | 1 | Sub Cat 3 |example.com| 1
配列を次のように生成できます。
Array (
[0] => Array (
[0] => 63
[site_id] => 63
[1] => 0
[parent_id] => 0
[2] => Category 1
[site_name] => Category 1
[3] => Link1
[site_url] => Link1
[4] => 0
[level] => 0
[children] => Array (
[0] => Array (
[0] => 66
[site_id] => 66
[1] => 63
[parent_id] => 63
[2] => Sub Cat 3
[site_name] => Sub Cat 3
[3] =>
[site_url] =>
[4] => 1
[level] => 1
[children] => Array ( )
)
[1] => Array (
[0] => 64
[site_id] => 64
[1] => 63
[parent_id] => 63
[2] => Sub Cat 1
[site_name] => Sub Cat 1
[3] =>
[site_url] =>
[4] => 2
[level] => 2
[children] => Array (
[0] => Array (
[0] => 65
[site_id] => 65
[1] => 64
[parent_id] => 64
[2] => Sub Cat2
[site_name] => Sub Cat2
[3] =>
[site_url] =>
[4] => 1
[level] => 1
[children] => Array ( )
)
)
)
)
)
)
https://stackoverflow.com/a/2795069/1137983の関数を使用します。投稿の getTopCategories() と getCategories 関数のみを使用しました
出力を生成するために、次を使用します。
{foreach from=$sitemap item=c name=sitemap}
{if $c.level==0 }
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{foreach item=d from=$c.children name=sitemap}
<li><a title="{$d.site_name}" href="{$d.site_url}">{$d.site_name}</a></li>
{/foreach}
{else}
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{/if}
</ul>
</li>
{/foreach}
</ul>