新零云博客 - 专注活动,软件,教程分享!

新零云博客
XingLy.Cn
ad

[原创收集] Typecho常用代码 - 精选版

请输入图片描述

首页置顶文章

<?php
$sticky = '3'; //置顶的文章cid,按照排序输入, 请以半角逗号或空格分隔
if($sticky && $this->is('index') || $this->is('front')){
$sticky_cids = explode(',', strtr($sticky, ' ', ','));//分割文本 
$sticky_html = "<span style='color:red'>[置顶] </span>"; //置顶标题的 html
$db = Typecho_Db::get();
$pageSize = $this->options->pageSize;
$select1 = $this->select()->where('type = ?', 'post');
$select2 = $this->select()->where('type = ? && status = ? && created < ?', 'post','publish',time());
//清空原有文章的列队
$this->row = [];
$this->stack = [];
$this->length = 0;
$order = '';
foreach($sticky_cids as $i => $cid) {
    if($i == 0) $select1->where('cid = ?', $cid);
    else $select1->orWhere('cid = ?', $cid);
    $order .= " when $cid then $i";
    $select2->where('table.contents.cid != ?', $cid); //避免重复
}
if ($order) $select1->order(null,"(case cid$order end)"); //置顶文章的顺序 按 $sticky 中 文章ID顺序
if ($this->_currentPage == 1) foreach($db->fetchAll($select1) as $sticky_post){ //首页第一页才显示
    $sticky_post['sticky'] = $sticky_html;
    $this->push($sticky_post); //压入列队
}
$uid = $this->user->uid; //登录时,显示用户各自的私密文章
if($uid) $select2->orWhere('authorId = ? && status = ?',$uid,'private');
$sticky_posts = $db->fetchAll($select2->order('table.contents.created', Typecho_Db::SORT_DESC)- 
>page($this->_currentPage, $this->parameter->pageSize));
foreach($sticky_posts as $sticky_post) $this->push($sticky_post); //压入列队
$this->setTotal($this->getTotal()-count($sticky_cids)); //置顶文章不计算在所有文章内
}
?>

网站运行时间

// 设置时区
date_default_timezone_set('Asia/Shanghai');
function getBuildTime(){
// 在下面按格式输入本站创建的时间
$site_create_time = strtotime('2010-09-10 00:00:00');
$time = time() - $site_create_time;
if(is_numeric($time)){
    $value = array(
        "years" => 0, "days" => 0, "hours" => 0,
        "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
        $value["years"] = floor($time/31556926);
        $time = ($time%31556926);
    }
    if($time >= 86400){
        $value["days"] = floor($time/86400);
        $time = ($time%86400);
    }
    if($time >= 3600){
        $value["hours"] = floor($time/3600);
        $time = ($time%3600);
    }
    if($time >= 60){
        $value["minutes"] = floor($time/60);
        $time = ($time%60);
    }
    $value["seconds"] = floor($time);
    
    echo ''.$value['years'].'年'.$value['days'].'天'.$value['hours'].'小 
 时'.$value['minutes'].'分';
    }else{
        echo '';
    }
}

在文章中插入广告

其实就是判断查找文章的第一个p,然后,插入代码,放到functions里使用即可。

 function themeInit($archive) {

// 判断是否是文章,如果是就插入广告
$ad_code = '<div>这是你的广告</div>';
if ($archive->is('single')) {
     $archive->content = prefix_insert_after_paragraph( $ad_code, 2, $archive->content );;
 }
 }

// 插入广告所需的功能代码
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
 $closing_p = '</p>';
 $paragraphs = explode( $closing_p, $content );
 foreach ($paragraphs as $index => $paragraph) {
  if ( trim( $paragraph ) ) {
     $paragraphs[$index] .= $closing_p;
  }
  if ( $paragraph_id == $index + 1 ) {
     $paragraphs[$index] .= $insertion;
  }
 }
 return implode( '', $paragraphs );
}

Typecho Nginx 伪静态规则

适用于nginx使用。军哥lnmp内置:

    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
    }

apache规则:

RewriteEngine On 
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] 

本原创文章未经允许不得转载 | 当前页面:新零云博客 - 专注活动,软件,教程分享! » [原创收集] Typecho常用代码 - 精选版

评论