This commit is contained in:
杜恒 2021-02-19 15:30:59 +08:00
parent 81b8d46129
commit 12a18f414b
4 changed files with 61 additions and 2 deletions

View File

@ -71,6 +71,12 @@ function themeInit($self)
break; break;
}; };
} }
/* 增加自定义sitemap功能 */
if ($self->request->getRequestUri() == "/sitemap.xml" || $self->request->getRequestUri() == "/index.php/sitemap.xml") {
$self->setThemeFile("library/sitemap.php");
$self->response->setStatus(200);
}
} }
/* 增加自定义字段 */ /* 增加自定义字段 */

View File

@ -3,7 +3,7 @@
/* 获取主题当前版本号 */ /* 获取主题当前版本号 */
function _getVersion() function _getVersion()
{ {
return "5.1.9"; return "5.2.0";
}; };
/* 判断是否是手机 */ /* 判断是否是手机 */

View File

@ -195,7 +195,8 @@ function themeConfig($form)
$JFooter_Right = new Typecho_Widget_Helper_Form_Element_Textarea( $JFooter_Right = new Typecho_Widget_Helper_Form_Element_Textarea(
'JFooter_Right', 'JFooter_Right',
NULL, NULL,
'<a href="https://as.js.cn/feed/" target="_blank" rel="noopener noreferrer">RSS</a>', '<a href="https://as.js.cn/feed/" target="_blank" rel="noopener noreferrer">RSS</a>
<a href="https://as.js.cn/sitemap.xml" target="_blank" rel="noopener noreferrer" style="margin-left: 15px">MAP</a>',
'自定义底部栏右侧内容(非必填)', '自定义底部栏右侧内容(非必填)',
'介绍:用于修改全站底部右侧内容 <br> '介绍:用于修改全站底部右侧内容 <br>
例如:&lt;a href="/"&gt;首页&lt;/a&gt; &lt;a href="/"&gt;关于&lt;/a&gt;' 例如:&lt;a href="/"&gt;首页&lt;/a&gt; &lt;a href="/"&gt;关于&lt;/a&gt;'

52
library/sitemap.php Normal file
View File

@ -0,0 +1,52 @@
<?php
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');
$pages = $db->fetchAll($db->select()->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $options->gmtTime)
->where('table.contents.type = ?', 'page')
->order('table.contents.created', Typecho_Db::SORT_DESC));
$articles = $db->fetchAll($db->select()->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $options->gmtTime)
->where('table.contents.type = ?', 'post')
->order('table.contents.created', Typecho_Db::SORT_DESC));
header("Content-Type: application/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
foreach ($pages as $page) {
$type = $page['type'];
$routeExists = (NULL != Typecho_Router::get($type));
$page['pathinfo'] = $routeExists ? Typecho_Router::url($type, $page) : '#';
$page['permalink'] = Typecho_Common::url($page['pathinfo'], $options->index);
echo "\t<url>\n";
echo "\t\t<loc>" . $page['permalink'] . "</loc>\n";
echo "\t\t<lastmod>" . date('Y-m-d\TH:i:s\Z', $page['modified']) . "</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.8</priority>\n";
echo "\t</url>\n";
}
foreach ($articles as $article) {
$type = $article['type'];
$article['categories'] = $db->fetchAll($db->select()->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $article['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC));
$article['category'] = urlencode(current(Typecho_Common::arrayFlatten($article['categories'], 'slug')));
$article['slug'] = urlencode($article['slug']);
$article['date'] = new Typecho_Date($article['created']);
$article['year'] = $article['date']->year;
$article['month'] = $article['date']->month;
$article['day'] = $article['date']->day;
$routeExists = (NULL != Typecho_Router::get($type));
$article['pathinfo'] = $routeExists ? Typecho_Router::url($type, $article) : '#';
$article['permalink'] = Typecho_Common::url($article['pathinfo'], $options->index);
echo "\t<url>\n";
echo "\t\t<loc>" . $article['permalink'] . "</loc>\n";
echo "\t\t<lastmod>" . date('Y-m-d\TH:i:s\Z', $article['modified']) . "</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.5</priority>\n";
echo "\t</url>\n";
}
echo "</urlset>";