typecho引入php文件,Typecho 各种调用 - 资本家

Typecho调用分类文章列表:

编辑当前typecho主题模板,在要调用某分类目录的位置添加以下代码

widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=3')

->parse('

其中pageSize后面的数字表示调用文章的数量;mid后面的数字表示调用的分类ID;

提示:Typecho分类目录ID的获取方法是把鼠标移到某分类名称上面,在浏览器状态栏显示的mid=后面的数字便是该分类目录ID。

1.自定义一下标题,以下为代码和参考案例:

_currentPage>1) echo '第 '.$this->_currentPage.' 页 - '; ?>archiveTitle('', '', ' - '); ?>options->title(); ?>

is('index')): ?> - 自定义关键词

2.根据TAG调用相关文章:

related(5)->to($relatedPosts); ?>

next()): ?>

3.上一篇与下一篇调用代码:

thePrev(); ?> theNext(); ?>

4.全部文章列表,可应用于归档或网站地图,蜘蛛指引等:

widget('Widget_Contents_Post_Recent', 'pageSize=10000')->parse('

{year}-{month}-{day} : {title}'); ?>

5.全部标签列表,按照MID排序:

widget('Widget_Metas_Tag_Cloud')

->to($taglist); ?>next()): ?>

6.自定义标签数量(就这里面的20),按照文章数量排序:

widget('Widget_Metas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => 20))->to($tags); ?>

next()): ?>

7.自定义分类、标签、搜索、首页等文章分页数量,修改 functions.php 文件:

function themeInit($archive) {

if ($archive->is('index')) {

$archive->parameter->pageSize = 10; // 自定义条数

}

}

或者:

function themeInit($archive) {

if ($archive->is('category', 'default')) {

$archive->parameter->pageSize = 10; // 自定义条数

}

}

8.调用某分类文章,pageSize是数量,mid是分类号:

widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=47')

->parse('sequence == 0): ?>

//需要的插入

10.判断是否为首页,输出相关内容:

is('index')): ?>

//首页输出内容

//不是首页输出内容

11.判断当前分类,输出内容:

category == "help"): ?>

//当前分类为help缩略图,则输出内容。

12.首页不显示某分类内容:

next()): ?>

category != "cateslug"): ?>

//正常输出循环

13.Typecho侧边栏不显示博主评论,sidebar.php文件:

将以下代码:

widget('Widget_Comments_Recent')->to($comments); ?>

修改为:

widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>

14.例如24小时内发布的贴,需要一个标志来完成。这里是用判断输入特殊字符,再用CSS判断完成的。此代码由羽飞儿老师编写,案例可参考:www.weidao.net

/**

* 判断时间区间

*

* 使用方法 if(timeZone($this->date->timeStamp)) echo 'ok';

*/

function timeZone($from){

$now = new Typecho_Date(Typecho_Date::gmtTime());

return $now->timeStamp - $from < 24*60*60 ? true : false;

}

以上代码,加入到 functions.php 中,然后,在 index.php 中使用如下调用:

date->timeStamp)) echo ' new'; ?>

注:这样就会输出一个new的文字,可应用于class里,然后,自定义输出背景图片等。

15.自定义首页描述description文字内容:

is('index')): ?>

header('description=详细描述内容部分内容,自定义即可。'); ?>

header(); ?>

16.首页第一篇文章显示不一样:

_currentPage == 1) && ($this->sequence == 1)): ?>

... //首页第一篇文章

... //其它文章

17.导航菜单不显示某分类或某页面:

next()): ?>

next()): ?> //循环语句开始

slug != 'about'): ?> // 记得闭合 if 语句

slug != 'about') && ($pages->slug != 'links')): ?>

18.输出全部分类,并对当前分类current标记:

widget('Widget_Metas_Category_List')->to($category); ?>

next()): ?>

is('post')): ?>category == $category->slug): ?>is('category', $category->slug)): ?>>name(); ?>

19.像CMS那样,输出全部分类,并按分类输出文章:

/* 循环所有的分类 */

widget('Widget_Metas_Category_List')->to($categories); ?>

next()): ?>

/* 循环当前分类下的文章 */

widget('Widget_Archive@category-' . $categories->mid, 'pageSize=7&type=category', 'mid=' . $categories->mid)->to($posts); ?>

next()): ?>

sequence): ?> //判断第一篇文章

[name(); ?>]:title(43); ?>

excerpt(120, '...'); ?>

//文章列表

title(40); ?>

(commentsNum(); ?>)

20.前台输出相关统计:

to($stat); ?>

2011 年初建立以来,截至 %s 在已设定的 %s 个分类

和 %s 个页面中,

共发布了 %s 篇文章,并收到了 %s 条相关评论。

', date('Y年n月j日G时i分'), $stat->categoriesNum, $stat->publishedPagesNum, $stat->publishedPostsNum, $stat->publishedCommentsNum); ?>

21.调用单独页面评论代码,存在一个php,单独引用:

";

$db = $this->db; //Typecho_Db::get();

$options = $this->options; //Typecho_Widget::widget('Widget_Options');

$page = $db->fetchRow($db->select()->from('table.contents')

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

->where('table.contents.created < ?', $options->gmtTime)

->where('table.contents.slug = ?', $slug));

if ($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);

$comments = $db->fetchAll($db->select()->from('table.comments')

->where('table.comments.status = ?', 'approved')

->where('table.comments.created < ?', $options->gmtTime)

->where('table.comments.type = ?', 'comment')

->where('table.comments.cid ' . $isGuestbook . ' ?', $page['cid'])

->order('table.comments.created', Typecho_Db::SORT_DESC)

->limit($limit));

foreach ($comments AS $comment) {

echo '

No Comments";

}

//不需要结束标志,并空一行

22.自定义pagenv分页盒样式:

pageNav('上一页文字', '下一页文字', '默认显示数目', '省略符号'); ?>

23.创建自定义首页或页面模板:

widget('Widget_Archive@index', 'pageSize=4&type=category', 'mid=23')->to($indexpub); ?>

next()): ?>

permalink(); ?>

title() ?>

cid,4);?>

excerpt(80, '……'); ?>

25.自定义首页keywords和Description内容:

使用的是自定义的page模板做为首页,页page页面不输出关键词和描述。于是查阅了一下官方文档,得出以下结论,自定义这部分内容:

打开 header.php 文件:

header(); ?>

修改为:

is('index')): ?>

header('description=此处输入你的关键词'); ?>

header(); ?>

站点名称

1

options->title() ?>

域名地址

1

options->siteUrl(); ?>

后台地址

1

options->adminUrl(); ?>

完整路径标题,比如 文章 站点

1

archiveTitle(' » ', '', ' - '); ?>options->title(); ?>

站点说明

1

options->description() ?>

模版文件夹地址

1

options->themeUrl(); ?>

导入模版文件夹内php文件

1

need('.php'); ?>

作者名字

1

author(); ?>

当前登陆用户名字

1

user->screenName(); ?>

退出链接

1

rss地址

1

options->feedUrl(); ?>

作者头像

1

author->gravatar('200') ?>

此函数是完整该文作者全部文章列表链接

1

author->permalink(); ?>

该文作者个人主页链接

1

author->url(); ?>

该文作者的邮箱地址

1

author->mail(); ?>

1

1

rss评论

1

options->commentsFeedUrl(); ?>

引用模版文件夹内php文件

1

need('*.php'); ?>

获取最新post

1

widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('category(',', false); ?>

获取分类列表

1

2

3

4

widget('Widget_Metas_Category_List')

->parse('

{name} ({count})'); ?>

获取某分类post

1

2

3

4

5

widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')

->parse('

获取最新评论列表

1

2

3

4

5

6

widget('Widget_Comments_Recent')->to($comments); ?>

next()): ?>

author(false); ?>: excerpt(50, '...'); ?>

首页获取 最新文章 代码限制条数 (特别感谢蚂蚱)

1

2

3

4

5

next()): ?>

sequence <= 3): ?>

html

获取最新评论列表第二个版本,只显示访客评论不显示博主也就是作者或者说自己发的评论

1

2

3

4

widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>

next()): ?>

author(false); ?>: excerpt(50, '...'); ?>

获取文章时间归档

1

2

3

4

widget('Widget_Contents_Post_Date', 'type=month&format=F Y')

->parse('

获取标签集合,也就是标签云

1

2

3

4

widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?>

next()): ?>

name(); ?>

文章循环

1

2

3

next()): ?>

调用该文相关文章列表

1

2

3

4

5

6

7

8

related(5)->to($relatedPosts); ?>

have()): ?> //这句也可以写成 if (count($relatedPosts->stack))

next()): ?>

无相关文章

各种列表页面标题,如标签分类

1

archiveTitle('   ', '', ''); ?>

文章或页面,标题

1

title() ?>

文章上一篇

1

theNext(); ?>

文章下一篇

1

thePrev(); ?>

文章或页面,链接

1

permalink() ?>

文章或页面,发表时间

1

date(); ?>

文章或页面,评论数目

1

commentsNum('No Comments', '1 Comment', '%d Comments'); ?>

文章或页面,内容,括号里有内容,如果加入了more就会自动生成链接

1

content('阅读剩余部分...'); ?>

文章所在分类,链接形式

1

category(','); ?>

文章,所加标签

1

tags(', ', true, 'none'); ?>

列表页分页pageNav(); ?>

隐藏head区域的程序版本和模版名称

1

header("generator=&template="); ?>

获取读者墙

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

fetchAll(Typecho_Db::get()

->select('COUNT(author) AS cnt','author', 'url', 'mail')

->from('table.comments')

->where('created > ?', $period )

->where('status = ?', 'approved')

->where('type = ?', 'comment')

->where('authorId = ?', '0')

->group('author')

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

->limit(25)

);

$mostactive = '';

$avatar_path = 'http://www.gravatar.com/avatar/';

foreach ($counts as $count) {

$avatar = $avatar_path . md5(strtolower($count['mail'])) . '.jpg';

$c_url = $count['url']; if ( !$c_url ) $c_url = Helper::options()->siteUrl;

$mostactive .= "%22%20.%20%24avatar%20.%20%22\n";

}

echo $mostactive; ?>

登陆与未登录用户展示不同内容

1

2

3

4

5

user->hasLogin()): ?>

登陆可见

未登录和登陆均可见

导航页面列表调用隐藏特定的页面 这个演示隐藏了album和search两个页面

1

2

3

4

5

6

7

8

9

is('index')): ?>>主页

widget('Widget_Contents_Page_List')->to($pages); ?>

next()): ?>

slug != 'album') && ($pages->slug != 'search')): ?>

is('page', $pages->slug)): ?>>title(); ?>

Typecho归档页面(牧风提供,牧风演示:http://mufeng.me/archives)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);

$year=0; $mon=0; $i=0; $j=0;

$output = '

';

while($archives->next()):

$year_tmp = date('Y',$archives->created);

$mon_tmp = date('m',$archives->created);

$y=$year; $m=$mon;

if ($mon != $mon_tmp && $mon > 0) $output .= '';

if ($year != $year_tmp && $year > 0) $output .= '';

if ($year != $year_tmp) {

$year = $year_tmp;

$output .= '

'. $year .' 年'; //输出年份

}

if ($mon != $mon_tmp) {

$mon = $mon_tmp;

$output .= '

'. $mon .' 月'; //输出月份

}

$output .= '

'.date('d日: ',$archives->created).''. $archives->title .' ('. $archives->commentsNum.')'; //输出文章日期和标题

endwhile;

$output .= '';

echo $output;

?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值