21 7月 2013

Typecho 函数调用

Typecho调用分类文章列表:

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

 

<?php
$this->widget(‘Widget_Archive@index’, ‘pageSize=6&type=category’, ‘mid=3’)
->parse(‘<li><a href=”{permalink}”>{title}</a></li>’);
?>

 

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

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

 

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

<?php if($this->_currentPage>1) echo ‘第 ‘.$this->_currentPage.’ 页 – ‘; ?><?php $this->archiveTitle(”, ”, ‘ – ‘); ?><?php $this->options->title(); ?>
<?php if($this->is(‘index’)): ?> – 自定义关键词<?php endif; ?>

2.根据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; ?>
</ul>

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

 

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

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

<?php $this->widget(‘Widget_Contents_Post_Recent’, ‘pageSize=10000’)->parse(‘<li>{year}-{month}-{day} : <a href=”{permalink}”>{title}</a></li>’); ?>

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

<?php $this->widget(‘Widget_Metas_Tag_Cloud’)
->to($taglist); ?><?php while($taglist->next()): ?>
<li><a href=”<?php $taglist->permalink(); ?>” title=”<?php $taglist->name(); ?>”><?php $taglist->name(); ?></a></li>
<?php endwhile; ?>

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

<?php $this->widget(‘Widget_Metas_Tag_Cloud’, array(‘sort’ => ‘count’, ‘ignoreZeroCount’ => true, ‘desc’ => true, ‘limit’ => 20))->to($tags); ?>
<?php while($tags->next()): ?>
<li><a rel=”tag” href=”<?php $tags->permalink(); ?>”><?php $tags->name(); ?></a></li>
<?php endwhile; ?>

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是分类号:

<?php $this->widget(‘Widget_Archive@index’, ‘pageSize=6&type=category’, ‘mid=47’)
->parse(‘<li><a href=”{permalink}”>{title}</a></li>’); ?>

9.判断为当前页的第几篇文章,并单独输出代码,可应用于第一篇文章底部广告:

<?php if ($this->sequence == 0): ?>
//需要的插入
<?php endif; ?>

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

<?php if($this->is(‘index’)): ?>
//首页输出内容
<?php else: ?>
//不是首页输出内容
<?php endif; ?>

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

<?php if($this->category == “help”): ?>
//当前分类为help缩略图,则输出内容。
<?php endif; ?>

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

<?php while($this->next()): ?>
<?php if($this->category != “cateslug”): ?>
//正常输出循环
<?php endif; ?>
<?php endwhile; ?>

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

将以下代码:

<?php $this->widget(‘Widget_Comments_Recent’)->to($comments); ?>

修改为:

<?php $this->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 中使用如下调用:

<?php if(timeZone($this->date->timeStamp)) echo ‘ new’; ?>

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

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

<?php if($this->is(‘index’)): ?>
<?php $this->header(‘description=详细描述内容部分内容,自定义即可。’); ?>
<?php else: ?>
<?php $this->header(); ?><?php endif; ?>

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

<?php if (($this->_currentPage == 1) && ($this->sequence == 1)): ?>
… //首页第一篇文章
<?php else: ?>
… //其它文章
<?php endif; ?>

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

<?php while ($pages->next()): ?>
<?php while ($pages->next()): ?> //循环语句开始
<?php if ($pages->slug != ‘about’): ?> // 记得闭合 if 语句
<?php if (($pages->slug != ‘about’) && ($pages->slug != ‘links’)): ?>

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

<ul id=”nav_menu”>
<?php $this->widget(‘Widget_Metas_Category_List’)->to($category); ?>
<?php while ($category->next()): ?>
<li<?php if ($this->is(‘post’)): ?><?php if ($this->category == $category->slug): ?><?php endif; ?><?php else: ?><?php if ($this->is(‘category’, $category->slug)): ?><?php endif; ?><?php endif; ?>><a href=”<?php $category->permalink(); ?>” title=”<?php $category->name(); ?>”><?php $category->name(); ?></a></li>
<?php endwhile; ?>
</ul>

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

/* 循环所有的分类 */
<?php $this->widget(‘Widget_Metas_Category_List’)->to($categories); ?>
<?php while ($categories->next()): ?>

/* 循环当前分类下的文章 */
<?php $this->widget(‘Widget_Archive@category-‘ . $categories->mid, ‘pageSize=7&type=category’, ‘mid=’ . $categories->mid)->to($posts); ?>
<div>
<?php while ($posts->next()): ?>
<?php if (1 == $posts->sequence): ?> //判断第一篇文章
<h3>[<?php $categories->name(); ?>]:<a href=”<?php $posts->permalink(); ?>”><?php $posts->title(43); ?></a></h3>
<?php $posts->excerpt(120, ‘…’); ?>
<ul> //文章列表
<?php else: ?>
<li>
<a href=”<?php $posts->permalink(); ?>”><?php $posts->title(40); ?></a>
<span>(<?php $posts->commentsNum(); ?>)</span>
</li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
<?php endwhile; ?>

20.前台输出相关统计:

<?php Typecho_Widget::widget(‘Widget_Stat’)->to($stat); ?>
<p><?php _e(‘不烦恼的博客自 <strong>2011</strong> 年初建立以来,截至 %s 在已设定的 <strong>%s</strong> 个分类
和 <strong>%s</strong> 个页面中,
共发布了 <strong>%s</strong> 篇文章,并收到了 <strong>%s</strong> 条相关评论。
‘, date(‘Y年n月j日G时i分’), $stat->categoriesNum, $stat->publishedPagesNum, $stat->publishedPostsNum, $stat->publishedCommentsNum); ?></p>

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

<?php
/**
* 单独页面调用评论列表
*
* @author Mr.Asong
* @link http://mrasong.com
*/
$slug = “message”; //页面缩略名
$limit = 10; //调用数量
$length = 30; //截取长度
$ispage = true; //true 输出slug页面评论,false输出其它所有评论
$isGuestbook = $ispage ? ” = ” : ” <> “;

$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 ‘<li>’;
echo ‘<a href=”‘ . $page[‘permalink’] . “#comment-” . $comment[‘coid’] . ‘” title=”‘ . $comment[‘text’] . ‘”>’;
echo Typecho_Common::subStr(strip_tags($comment[‘text’]), 0, $length, ‘…’) . ‘</a>’;
echo ‘</li>’;
}
} else {
echo “<li>No Comments</li>”;
}
//不需要结束标志,并空一行

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

<?php $this->pageNav(‘上一页文字’, ‘下一页文字’, ‘默认显示数目’, ‘省略符号’); ?>

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

<?php
/**
* 自定义首页模板
*
* @package index
*/

<?php
/**
* 自定义页面模板
*
* @package custom
*/

24.自定义调用某分类,并输出缩略图:

<?php $this->widget(‘Widget_Archive@index’, ‘pageSize=4&type=category’, ‘mid=23’)->to($indexpub); ?>
<?php while($indexpub->next()): ?>
<?php $indexpub->permalink(); ?>
<?php $indexpub->title() ?>
<?php Fimg_Plugin::showfimg($indexpub->cid,4);?>
<?php $indexpub->excerpt(80, ‘……’); ?>
<?php endwhile; ?>

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

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

打开 header.php 文件:

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

修改为:

<?php if($this->is(‘index’)): ?>
<?php $this->header(‘description=此处输入你的关键词’); ?>
<?php else: ?>
<?php $this->header(); ?><?php endif; ?>

 

 
站点名称

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

域名地址

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

后台地址

1
<?php $this->options->adminUrl(); ?>

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

1
<?php $this->archiveTitle(' &raquo; ', '', ' - '); ?><?php $this->options->title(); ?>

站点说明

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

模版文件夹地址

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

导入模版文件夹内php文件

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

作者名字

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

当前登陆用户名字

1
<?php $this->user->screenName(); ?>

退出链接

1
<a href="<?php $this->options->logoutUrl(); ?>"><?php _e('退出'); ?></a>

rss地址

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

作者头像

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

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

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

该文作者个人主页链接

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

该文作者的邮箱地址

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

 

1

 

 

1

 

rss评论

1
<?php $this->options->commentsFeedUrl(); ?>

引用模版文件夹内php文件

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

获取最新post

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

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

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

获取分类列表

1
2
3
4
<ul>
<?php $this->widget('Widget_Metas_Category_List')
                ->parse('<li><a href="{permalink}">{name}</a> ({count})</li>'); ?>
</ul>

获取某分类post

1
2
3
4
5
<ul>
<?php 
$this->widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')
->parse('<li><a href="{permalink}" title="{title}">{title}</a></li>'); ?>
</ul>

获取最新评论列表

1
2
3
4
5
6
<ul>
            <?php $this->widget('Widget_Comments_Recent')->to($comments); ?>
            <?php while($comments->next()): ?>
                <li><a href="<?php $comments->permalink(); ?>"><?php $comments->author(false); ?></a>: <?php $comments->excerpt(50, '...'); ?></li>
            <?php endwhile; ?>
            </ul>

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

1
2
3
4
5
<?php while ($this->next()): ?>
<?php if ($this->sequence <= 3): ?>
html
<?php endif; ?>
<?php endwhile; ?>

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

1
2
3
4
<?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>
    <?php while($comments->next()): ?>
	<li><a href="<?php $comments->permalink(); ?>"><?php $comments->author(false); ?></a>: <?php $comments->excerpt(50, '...'); ?></li>
<?php endwhile; ?>

获取文章时间归档

1
2
3
4
<ul>
                <?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')
                ->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
            </ul>

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

1
2
3
4
<?php $this->widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?>
<?php while($tags->next()): ?>
<a href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a>
<?php endwhile; ?>

文章循环

1
2
3
<?php while($this->next()): ?>
<!--文章标题内容等-->
<?php endwhile; ?>

调用该文相关文章列表

1
2
3
4
5
6
7
8
<?php $this->related(5)->to($relatedPosts); ?>
    <?php if ($relatedPosts->have()): ?>    //这句也可以写成 if (count($relatedPosts->stack))
    <?php while ($relatedPosts->next()): ?>
        <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
    <?php endwhile; ?>
    <?php else : ?>
        <li>无相关文章</li>
    <?php endif; ?>

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

1
<?php $this->archiveTitle(' &nbsp; ', '', ''); ?>

文章或页面,标题

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

文章上一篇

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

文章下一篇

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

文章或页面,链接

1
<?php $this->permalink() ?>

文章或页面,发表时间

1
<?php $this->date(); ?>

文章或页面,评论数目

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

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

1
<?php $this->content('阅读剩余部分...'); ?>

文章所在分类,链接形式

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

文章,所加标签

1
<?php $this->tags(', ', true, 'none'); ?>

列表页分页pageNav(); ?>
隐藏head区域的程序版本和模版名称

1
<?php $this->header("generator=&template="); ?>

获取读者墙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$period = time() - 999592000; // 時段: 30 天, 單位: 秒
$counts = Typecho_Db::get()->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 .= "<a href='" . $c_url . "' title='" . $count['author'] . " (参与" . $count['cnt'] . "次互动)' target='_blank'><img src='" . $avatar . "' alt='" . $count['author'] . "的头像' class='avatar' width='32' height='32' /></a>\n";
}
echo $mostactive; ?>

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

1
2
3
4
5
<?php if($this->user->hasLogin()): ?>
					登陆可见
                <?php else: ?>
                    未登录和登陆均可见
                <?php endif; ?>

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

1
2
3
4
5
6
7
8
9
<ul>
<li<?php if($this->is('index')): ?><?php endif; ?>><a href="<?php $this->options->siteUrl(); ?>">主页</a></li>
<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
	<?php while($pages->next()): ?>
	<?php if (($pages->slug != 'album') && ($pages->slug != 'search')): ?>
    <li<?php if($this->is('page', $pages->slug)): ?><?php endif; ?>><a href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>"><?php $pages->title(); ?></a></li>
	<?php endif; ?>
    <?php endwhile; ?>
</ul>

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
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
	$year=0; $mon=0; $i=0; $j=0;
	$output = '<div id="archives">';
	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 .= '</ul></li>';
		if ($year != $year_tmp && $year > 0) $output .= '</ul>';
		if ($year != $year_tmp) {
			$year = $year_tmp;
			$output .= '<h3>'. $year .' 年</h3><ul>'; //输出年份
		}
		if ($mon != $mon_tmp) {
			$mon = $mon_tmp;
			$output .= '<li><span>'. $mon .' 月</span><ul>'; //输出月份
		}
		$output .= '<li>'.date('d日: ',$archives->created).'<a href="'.$archives->permalink .'">'. $archives->title .'</a> <em>('. $archives->commentsNum.')</em></li>'; //输出文章日期和标题
	endwhile;
	$output .= '</ul></li></ul></div>';
	echo $output;
?>

 

5 comments

  1. That’s a good post.

  2. It’s a good post.

  3. 博主,麻烦问一下。那个
    widget(‘Widget_Archive@index’, ‘pageSize=6&type=category’, ‘mid=47’)
    ->parse(‘{title}‘); ?>
    这个调用代码中 ….中间不能用其它调用标签吗,我把缩略图调用标签放到这里面,没得效果。谢谢。

    • 肯定可以调用的。注意学习下php的插入语法。我提供一个,你试试,我没测试哟,不一定对。《li》
      这里写缩略图
      < ?php $this->widget(‘Widget_Archive@index’, ‘pageSize=6&type=category’, ‘mid=3’)
      ->parse(‘{title}‘);
      ?>
      或者写这里。注意加php插入标签。

      《/li》

  4. widget(‘Widget_Archive@index’, ‘pageSize=8&type=category’, ‘mid=1’)->to($indexpub); ?> 我想通过后台用户配置的mid来操作,这个mid该如何写?后台获取的mid的代码是这样的options->mid() ?>