Typecho $this->category 和 $this->categories 的区别

前言

  • 做主题的时候,经常用到 $this->category$this->category()$this->categories
  • 在文章详情页,$this->category 输出最靠前的一个直属分类,$this->categories 输出所有分类
  • 在 archive 页面,也就是类似 /category/typecho 的页面,$this->categories 的输出和想的不太一样

构造一个场景

  • 假设有以下三个分类

    • 分类 1:slug 为 typecho
    • 分类 2:slug 为 php
    • 分类 3:slug 为 websites,且是分类 1 、分类 2 的父分类
  • 分类 3 的 mid 和 order 最小
  • 假设有一篇文章,勾选的分类为 分类 1、分类 2、分类 3,也就是三个分类全勾了

文章详情页的输出结果

  • var_export($this->category);
'websites'

  • var_export($this->categories);
array ( 
    0 => array ( 
        'mid' => '6', 
        'name' => 'TYPECHO', 
        'slug' => 'typecho', 
        'type' => 'category', 
        'description' => '', 
        'count' => '4', 
        'order' => '1', 
        'parent' => '1', 
        'cid' => '50', 
        'directory' => array ( 
            0 => 'websites', 
            1 => 'typecho', 
        ), 
        'permalink' => 'https://paoti.club/category/typecho/', 
        'feedUrl' => 'https://paoti.club/feed/category/typecho/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/typecho/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/typecho/', 
    ), 
    1 => array ( 
        'mid' => '1', 
        'name' => '网站&服务器', 
        'slug' => 'websites', 
        'type' => 'category', 
        'description' => '', 
        'count' => '5', 
        'order' => '1', 
        'parent' => '0', 
        'cid' => '50', 
        'directory' => array ( 
            0 => 'websites', 
        ), 
        'permalink' => 'https://paoti.club/category/websites/', 
        'feedUrl' => 'https://paoti.club/feed/category/websites/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/websites/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/websites/', 
    ), 
    2 => array ( 
        'mid' => '26', 
        'name' => 'PHP', 
        'slug' => 'php', 
        'type' => 'category', 
        'description' => '', 
        'count' => '3', 
        'order' => '4', 
        'parent' => '1', 
        'cid' => '50', 
        'directory' => array ( 
            0 => 'websites', 
            1 => 'php', 
        ), 
        'permalink' => 'https://paoti.club/category/php/', 
        'feedUrl' => 'https://paoti.club/feed/category/php/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/php/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/php/', 
    ), 
)

/categort/typecho 页面的输出结果

输出的是当前分类页面下文章关联的分类,而不是当前分类

  • var_export($this->category);
'websites'

  • var_export($this->categories);
array ( 
    0 => array ( 
        'mid' => '1', 
        'name' => '网站&服务器', 
        'slug' => 'websites', 
        'type' => 'category', 
        'description' => '', 
        'count' => '5', 
        'order' => '1', 
        'parent' => '0', 
        'cid' => '46', 
        'directory' => array ( 
            0 => 'websites', 
        ), 
       'permalink' => 'https://paoti.club/category/websites/', 
        'feedUrl' => 'https://paoti.club/feed/category/websites/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/websites/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/websites/', 
    ), 
    1 => array ( 
        'mid' => '6', 
        'name' => 'TYPECHO', 
        'slug' => 'typecho', 
        'type' => 'category', 
        'description' => '', 
        'count' => '4', 
        'order' => '1', 
        'parent' => '1', 
        'cid' => '46', 
        'directory' => array ( 
            0 => 'websites', 
            1 => 'typecho', 
        ), 
        'permalink' => 'https://paoti.club/category/typecho/', 
        'feedUrl' => 'https://paoti.club/feed/category/typecho/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/typecho/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/typecho/', 
    ), 
    2 => array ( 
        'mid' => '30', 
        'name' => '云产品', 
        'slug' => 'cloud', 
        'type' => 'category', 
        'description' => '', 
        'count' => '1', 
        'order' => '3', 
        'parent' => '1', 
        'cid' => '46', 
        'directory' => array ( 
            0 => 'websites', 
            1 => 'cloud', 
        ), 
        'permalink' => 'https://paoti.club/category/cloud/', 
        'feedUrl' => 'https://paoti.club/feed/category/cloud/', 
        'feedRssUrl' => 'https://paoti.club/feed/rss/category/cloud/', 
        'feedAtomUrl' => 'https://paoti.club/feed/atom/category/cloud/',
    ), 
)