fastadmincms资讯文章时间归档

PHP开源项目 / 177人浏览 / 0人评论

php时间归档就是查找时间范围内的资讯文字,那么thinkphp5.0极速框架fastadmin如何实现时间归档,时间归档首先查询所有资讯和文章所在的各个年份,过滤掉重复年月就可的到当前站点文章在哪些年份发布过文章

fastadmin php极速框架

php cms等插件时间时间归档

php时间归档就是查找时间范围内的资讯文字,那么thinkphp5.0极速框架fastadmin如何实现时间归档?
时间归档首先查询所有资讯和文章所在的各个年份,过滤掉重复年月就可的到当前站点文章在哪些年份发布过文章。
这里仅仅实现年份,如果想要统计月份,可自行扩展。

fastadmin插件(cms)标签定义获取年份(有哪些年份)

fastadmin cms 标签库中加入

'channelyear'     => ['attr' => 'id,row,empty,key,model', 'close' => 1],
'channelmonth'    => ['attr' => 'id,row,empty,key,model', 'close' => 1],
/**
 * 年份归档
 * @param array  $tag
 * @param string $content
 * @return string
 */
public function tagChannelyear($tag, $content)
{
    $id = $tag['id'];
    $empty = isset($tag['empty']) ? $tag['empty'] : '';
    $key = !empty($tag['key']) ? $tag['key'] : 'i';
    $mod = isset($tag['mod']) ? $tag['mod'] : '2';
    $params = [];
    
    // 频道、模型、条件、标签暂时不做(仅仅只实现年份链接)
    $var = Random::alnum(10);
    $parse = '<?php ';
    $parse .= '$__' . $var . '__ = \addons\cms\model\Archives::getArchivesYearList([' . implode(',', $params) . ']);';
    $parse .= ' ?>';
    $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
    $parse .= $content;
    $parse .= '{/volist}';
    $parse .= '{php}$__LASTLIST__=$__' . $var . '__;{/php}';
    return $parse;
}    
/**
 * 处理年份归档
 * @param $tag
 * @return array|false|\PDOStatement|string|\think\Collection
 */
public static function getArchivesYearList($tag)
{
    $model = !isset($tag['model']) ? '' : $tag['model'];
    $field = empty($tag['field']) ? 'createtime' : $tag['field'];
    $addon = empty($tag['addon']) ? false : $tag['addon'];
    $where = ['status' => 'normal'];
    $where['deletetime'] = ['exp', Db::raw('IS NULL')]; //by erastudio
    if ($model !== '') {
        $where['model_id'] = ['in', $model];
    }

    $modelInfo = null;

    $archivesModel = self::where($where)
        ->field($field);
        
    $list = $archivesModel->select();
    
    foreach ($list as $key => $value) {
        if($value['createtime']){
            $time['year'][] = date('Y', $value['createtime']);
            $time['month'][] = date('F Y', $value['createtime']);
        }
    }
    $year = array_reverse(array_unique($time['year']));
    
    $nowTime = time();
    $yearTime = [];
    foreach ($year as $key=>$item) {
        $vars = [
            ':id'       => $item,
            ':diyname'  => $item,
            ':channel'  => $item,
            ':catename' => $item,
            ':cateid'   => $item,
            ':year'     => $item,
            ':month'    => date("m", $nowTime),
            ':day'      => date("d", $nowTime),
        ];
        $suffix = static::$config['moduleurlsuffix']['archives'] ?? static::$config['urlsuffix'];
        $yearTime[$key]['url'] = addon_url('cms/channel/year', $vars, $suffix, true);
        $yearTime[$key]['year_text'] = $item.'年';
    }
    
    return !empty($yearTime)?$yearTime:[];
}

标签实现年份归档

<!-- Y 时间归档 -->
<div class="panel panel-default hot-tags">
    <div class="panel-heading">
        <h3 class="panel-title">{:__('Archives Year')}</h3>
    </div>
    <div class="panel-body">
        <div class="tags">
            {cms:channelyear id="item"}
            <a href="{$item.url}" class="tag"> <span>{$item.year_text}</span></a>
            {/cms:channelyear}
        </div>
    </div>
</div>
<!-- E 时间归档 -->

转载注明:

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我