在Typecho的主题一览里是利用了一个循环来遍历主题,遍历主题的时候可以通过activated
参数来确定是否是当前主题,这样就可以利用官方提供的函数来获取主题信息了,缺点很明显,需要多使用一个循环,浪费资源。
<?php \Widget\Themes\Rows::alloc()->to($themes); ?>
<?php while ($themes->next()): ?>
<?php if ($themes->activated && !$options->missingTheme): ?>
<?php $themes->screen(); ?><br>
<?php $themes->name(); ?><br>
<?php $themes->title(); ?><br>
<?php $themes->homepage(); ?><br>
<?php $themes->author(); ?><br>
<?php $themes->version(); ?><br>
<?php $themes->description(); ?><br>
<?php endif; ?>
<?php endwhile; ?>
因为主要是想提取其中的info信息,而这个信息依赖Plugin.php
文件,于是尝试跳过循环,直接找到当前的主题名,然后通过拼接找到当前主题的index文件:
use Typecho\Plugin;
$currentTheme = $this->options->theme;
$themeFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . $currentTheme . '/index.php';
$info = Plugin::parseInfo($themeFile);
print_r($info); // 打印主题信息
上面这个路径的获取还是有点厄长,如果把这段程序放在同文件内的functions.php中,因为是与index.php在同一个路径下,我们也可以修改成这样:
use Typecho\Plugin;
$themeFile = __DIR__ . '/index.php';
$info = Plugin::parseInfo($themeFile);
print_r($info); // 打印主题信息
白天在泽泽社长整理的typecho文档里看到更简洁的实现方式,摘录如下:
$info = Typecho_Plugin::parseInfo(__DIR__ . '/index.php');
return $info['version'];
info数组允许包含的信息列表:
/** 初始信息 */
$info = [
'description' => '',
'title' => '',
'author' => '',
'homepage' => '',
'version' => '',
'since' => '',
'activate' => false,
'deactivate' => false,
'config' => false,
'personalConfig' => false
];