typecho 文章内运行php,typecho常用文章数据调用方法,汇总整理

此文章内包括typecho热门文章调用,热评文章调用,随机调用,字数排行,根据指定id调用,调用相关文章,根据分类调用,以及根据标签调用的方法,看起来是不是特别的全,基本汇总了网上所有的方法,不过值得一提的是,我提供的方法自由度更高,而不是直接像常用的那样,把html的标签代码都写在了function.php里面。好吧,我承认是之前写过一篇这样的文章,typecho二级开发实例,添加小部件接口,然后看到qq爹博客大佬里这样的文章,Typecho自定义调用如热门文章随机文章等,顿时觉得惊为天人,原来wdget也是可以直接在功能,php里面定义的。

于是这篇文章就刚刚诞生了,做个汇总也好,以后开发模板直接来复制。

694571426.jpg

调用热评文章:class Widget_Contents_Post_Comments extends Widget_Abstract_Contents

{

public function execute()

{

$this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));

$this->db->fetchAll($this->select()

->where('table.contents.status = ?', 'publish')

->where('table.contents.created options->time)

->where('table.contents.type = ?', 'post')

->order('commentsNum', Typecho_Db::SORT_DESC)

->limit($this->parameter->pageSize), array($this, 'push'));

}

}

前台模板写法:<?php  $this->widget('Widget_Contents_Post_Comments','pageSize=10')->to($Comments);while($Comments->next()): ?>

循环内代码

调用热门文章(注意先实现阅读量功能):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);while($hot->next()): ?>

循环内代码

召集随机文章:class Widget_Contents_Post_Rand extends Widget_Abstract_Contents

{

public function execute()

{

$this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));

$this->db->fetchAll($this->select()

->where('table.contents.status = ?', 'publish')

->where('table.contents.created options->time)

->where('table.contents.type = ?', 'post')

->order('RAND()', Typecho_Db::SORT_DESC)

->limit($this->parameter->pageSize), array($this, 'push'));

}

}

前台模板写法:

<?php $this->widget('Widget_Contents_Post_Rand','pageSize=10')->to($Rand);while($Rand->next()): ?>

循环内代码

根据字数调用:class Widget_Contents_Post_Size extends Widget_Abstract_Contents

{

public function execute()

{

$this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));

$this->db->fetchAll($this->select()

->where('table.contents.status = ?', 'publish')

->where('table.contents.created options->time)

->where('table.contents.type = ?', 'post')

->order('LENGTH(text)', Typecho_Db::SORT_DESC)

->limit($this->parameter->pageSize), array($this, 'push'));

}

}

前台模板写法:<?php  $this->widget('Widget_Contents_Post_Size','pageSize=10')->to($Size);while($Size->next()): ?>

循环内代码

调用指定文章: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";

$this->widget('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju);while($fanju->next()): ?>

循环内代码

调用相关文章(文章详情页面使用)<?php  $this->related(6)->to($relatedPosts); ?>

循环内代码

据分类调用文章:<?php  $this->widget('Widget_Archive@index1', 'pageSize=5&type=category', 'mid=分类ID')->to($categoryPosts);while($categoryPosts->next()):?>

循环内代码

根据标签调用:<?php  $this->widget('Widget_Archive@indexessence', 'pageSize=2&type=tag', 'slug=精华')->to($indexessence);while($indexessence->next()): ?>

循环内代码

the-end.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值