blog常用代码

整理一些常用的代码,这算是第一次上手PHP吧 ...


侧边栏函数修改位置

widget('Widget_Contents_Post_Recent')   var/Widget/Contents/Post/Recent.php

更换域名后
进入Typecho数据库找到Typecho_options表的siteurl,换成要改的域名。
然后执行下列sql 目的是更换图片路径

UPDATE typecho_contents SET text = REPLACE(text,'旧域名地址','新域名地址');

按照点击量排序

$select->order('table.contents.created', Typecho_Db::SORT_DESC)

修改为

$select->order('table.contents.views', Typecho_Db::SORT_DESC)

<?php if($this->category == "分类A的缩略名"): ?>
这里是分类A的文章页面HTML代码
<?php else: ?>
这里是分类B的文章页面HTML代码
<?php endif; ?>

常规调用

站点名称(其实就是调用title标签的内容的)

<?php $this->options->title() ?>

站点网址(其实就是调用首页名称的)

<?php $this->options ->siteUrl(); ?>

站点描述调用

<?php $this->options->description() ?>

完整路径标题(其实就是调用面包屑导航的)

<?php $this->archiveTitle(' &raquo; ', < span class="string">'', ' | '); ?><?php $this ->options->title(); ?>

模板文件夹地址(博主基本不用,因为不知道要这个是干嘛)

<?php $this->options->themeUrl(); ?>

输出指定的php文件,一般起关联作用,起到组合作用

<?php $this->need('global.php'); ?>

输出作者字段

<?php $this->author(); ?>

输出头像(此处40为img元素的宽度和高度)

<?php $this->author->gravatar('40') ?>

该文作者全部文章列表链接

<?php $this->author->permalink (); ?>

该文作者个人主页链接

<?php $this->author->url(); ?>

该文作者的邮箱地址

<?php $this->author->mail(); ?>

typecho 默认的文章上下翻页的调用方法

<?php $this->thePrev(); ?> 
<?php $this->theNext(); ?>

判断是否为首页,输出相关内容(博主不常用)

<?php if ($this->is('index')): ?>

//首页输出内容

<?php else: ?>

//不是首页输出内容

< span><?php endif; ?>

文章列表或页面,评论数目输出方法

<?php $this->commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>

截取部份文章简称“摘要”,200是字数限制

<?php $this->excerpt(200, '.. .'); ?>

调用自定义字段(官方文档坑爹,竟然没有,博主自己摸索出来的)

<?php $this->fields->fieldName ?>

RSS订阅地址输出方法

<?php $this->options->feedUrl(); ?>

获取最新post

<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>

纯文字分类名称,不带链接

<?php $this->category(',', false); ?>

非常实用!!文章调用 !自定义调用如热门文章随机文章等 在functions.php中加入如下代码 随机改为RAND()

class Widget_Post_hot extends Widget_Abstract_Contents
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }
    public function execute()
    {
        $select  = $this->select()->from('table.contents')
->where("table.contents.password IS NULL OR table.contents.password = ''")
->where('table.contents.status = ?','publish')
->where('table.contents.created <= ?', time())
->where('table.contents.type = ?', 'post')
->limit($this->parameter->pageSize)
->order('table.contents.views', Typecho_Db::SORT_DESC);
 $this->db->fetchAll($select, array($this, 'push'));
    }
}

然后在前台调用文章时就可以这样写了

<?php $this->widget('Widget_Post_hot@hot', 'pageSize=6')->to($hot); ?>
<?php while($hot->next()): ?>
文章链接:<?php $hot->permalink() ?>
文章标题:<?php $hot->title(); ?>
<!--等等-->
 <?php endwhile; ?>

调用指定文章集合 在functions.php中加入如下代码

class Widget_Post_fanjubiao extends Widget_Abstract_Contents
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }
    public function execute()
    {
        $select  = $this->select()->from('table.contents')
->where("table.contents.password IS NULL OR table.contents.password = ''")
->where('table.contents.type = ?', 'post')
->limit($this->parameter->pageSize)
->order('table.contents.modified', Typecho_Db::SORT_DESC);

if ($this->parameter->fanjubiao) {
$fanju=explode(",",$this->parameter->fanjubiao);
$select->where('table.contents.cid in ?', $fanju);
}
 $this->db->fetchAll($select, array($this, 'push'));
    }
}

然后在前台调用热门文章时就可以这样写了

<?php 
$week1="728,1197";//指定文章id集合多个文章中间用英文逗号隔开
$this->widget('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju); ?>
<?php while($fanju->next()): ?>
文章链接:<?php $fanju->permalink() ?>
文章标题:<?php $fanju->title(); ?>
<!--等等-->
 <?php endwhile; ?>

调用分类文章

<?php $this->widget('Widget_Archive@fenlei', 'pageSize=6&type=category', 'mid=1')->to($new); ?>
<?php while ($new->next()): ?>
<a href="<?php $new->permalink(); ?>"><?php $new->title(); ?></a>
<?php endwhile; ?>

以上就是获取分类mid等于1的最新6篇文章,pageSize=6就是指定调用数量,mid=1指定分类mid,也可以用缩略名方式替换如slug=name其中name就是mid等于1的分类的缩略名。

调用标签文章

<?php $this->widget('Widget_Archive@biaoqian', 'pageSize=6&type=tag', 'mid=1')->to($new); ?>
<?php while ($new->next()): ?>
<a href="<?php $new->permalink(); ?>"><?php $new->title(); ?></a>
<?php endwhile; ?>

以上就是获取标签mid等于1的最新6篇文章,pageSize=6就是指定调用数量,mid=1指定标签mid,也可以用缩略名方式替换如slug=name其中name就是mid等于1的标签的缩略名。

调用相关文章tag

<?php $this->related(5)->to($relatedPosts); ?>
    <ul>
    <?php while ($relatedPosts->next()): ?>
    <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
    <?php endwhile; ?>

其中$this->related($limits, $type);里面可以使用两个参数,$limits默认值为 5,表示显示的相关文章数量,$type默认值为 NULL,表示文章的相关方式,只接受 author。当 $type 为 author 时,根据用户显示相关文章;为其他值时,根据标签显示相关文章。


Typecho中的is函数 进行页面判断

<?php if($this->is('index')): ?>
  <?php $this->need('search_box.php');?>
<?php else: ?>
  <?php $this->need('comment_section.php');?>
<?php endif;?>

is可用于哪些判断? is函数可以用于判断
index/archive/category/tag/date
/single/page/post/attachment
等,具体用法见下文描述。注意哈,这些页面是有相互包含的关系的,具体在使用过程中要多尝试下。

$this->is('index')
从字面可见,判断当前页面是否是首页
$this->is('archive')
判断当前页面是否是归档页,譬如主页,分类文章页,标签文章页,日期归档文章页
$this->is('category'),或者$this->is('category','some_slug')
判断当前页面是否为分类文章页,如果加第二个参数slug,则进一步判断是否为特定的分类,譬如默认分类的slug是“default”
$this->is('tag')或者$this->is('tag','some_slug')
判断当前页面是否是标签文章页,如果加第二个参数slug,则进一步精确判断,原理同category
$this->is('date')或者$this->is('date','some_range'),其中some_rage可以是year/month/day
判断当前页面是否是日期归档页,如果指定第二个参数,则进一步精确判断。
$this->is('single')
用于判断是否是内容页面,所谓内容页面,包括文章页、独立页面和附件显示页
$this->is('post')或者$this->is('post',$post_id)
用于判断是否是内容页,加第二个参数则进行精确判断
$this->is('page')或者$this->is('page','some_slug')
用于判断当前页面是否为独立页面,加第二个参数表示精确判断,譬如$this->is('page','about')则表示判断当前页面是否是about页面
$this->is('attachment')或者$this->is('attachment',$attachment_id)
同上,用于判断附件页面。

Typecho评论调用QQ头像 var/Typecho/Common.php

public static function gravatarUrl($mail, $size, $rating, $default, $isSecure = false)
    {
            $reg = "/^\d{5,11}@[qQ][Qq]\.(com)$/";
            if (preg_match($reg, $mail)) {
                $img    = explode("@", $mail);
                $url = "//q2.qlogo.cn/headimg_dl?dst_uin={$img[0]}&spec=100";
            } else {
                if (defined('__TYPECHO_GRAVATAR_PREFIX__')) {
                    $url = __TYPECHO_GRAVATAR_PREFIX__;
                } else {
                    $url = $isSecure ? 'https://secure.gravatar.com' : 'http://www.gravatar.com';
                    $url .= '/avatar/';
                }
                if (!empty($mail)) {
                    $url .= md5(strtolower(trim($mail)));
                }
                $url .= '?s=' . $size;
                $url .= '&amp;r=' . $rating;
                $url .= '&amp;d=' . $default;
            }
            return $url;
    }

禁用feed var/Widget/Archive.php

        $matched = Typecho_Router::match($this->request->feed, 'pageSize=0&isFeed=1');  /**  禁用feed 输出0**/
        if ($matched && $matched instanceof Widget_Archive) {
            $this->import($matched);
        } else {
            throw new Typecho_Widget_Exception(_t('聚合页不存在'), 404);
        }
    }

    /** 初始化聚合器 */
    $this->setFeed(new Typecho_Feed(Typecho_Common::VERSION, $this->_feedType, $this->options->charset, _t('zh-CN')));

    /** 默认输出10则文章 禁用feed 输出0**/
    $this->parameter->pageSize = 0; 
}

}


第三方文档 比官方的强多了

https://docs.qqdie.com/    资源站 https://qqdie.com/

橙汁

It was a summer afternoon.

Comments

  1. truncate typecho_comments , 清空ID计数 从1开始。 icon_twisted.gif

发表留言

icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
Smilies by QQ