Typecho一些魔改代码

chen'mo
2022-10-12 / 1 评论 / 11,803 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年10月12日,已超过741天没有更新,若内容或图片失效,请留言反馈。

侧栏文章增加排列序号

自定义字段:

<?php if (array_key_exists('image',unserialize($this->___fields()))): ?>
<span class="icon-image"></span>
<?php elseif (array_key_exists('music',unserialize($this->___fields()))): ?>
<span class="icon-music"></span>
<?php elseif (array_key_exists('play',unserialize($this->___fields()))): ?>
<span class="icon-play"></span>
<?php endif; ?> 

首页不显示某分类

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

调用相关文章

<?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>

调用指定分类文章

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

利用Typecho函数调用自定义分类内容显示方法
在模板的functions.php文件中,加入下面的代码

function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 8; // 自定义条数
}
}

同时设定两种不同分类列表下显示不同文章输出数量的实现办法

function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 8; // 自定义条数
}elseif($archive->is('category', 'news')){
$archive->parameter->pageSize = 12; // 自定义条数
}
}

Typecho 程序如何调用循环页面、分类、标签代码方法

<!--循环显示页面-->
<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
<?php while($pages->next()): ?>

<span class="nav-item<?php if($this->is('page', $pages->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>">
<span><?php $pages->title(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示页面-->

<!--循环所有分类-->
<?php $this->widget('Widget_Metas_Category_List')->to($category); ?>
<?php while ($category->next()): ?>

<span class="nav-item<?php if($this->is('category', $category->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>">
<span><?php $category->name(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示分类-->

<!--循环显示标签 其中limit的5为显示数量-->
<?php $this->widget('Widget_Metas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => 5))->to($tags); ?>
<?php while($tags->next()): ?>

<span class="nav-item<?php if($this->is('tag', $tags->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $tags->permalink(); ?>" title="<?php $tags->name(); ?>">
<span><?php $tags->name(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示标签-->

不同分类调用不同的模板页面

<?php
if($this->is('category','default')){
$this->need('default.php');
}elseif($this->is('category','technology')){
$this->need('technology.php');
}else{
$this->need('other.php');
}
?>

或者

<?php
$slugArray = array('default','technology');
foreach($slugArray as $slug){
if($this->is('category',$slug)){
$this->need('default.php');
}else{
$this->need('other.php');
}
}
?>

Typecho最新24小时内文章加上NEW标签方法

/**
 * 时间函数
 * 
  */
function timeZone($from){
$now = new Typecho_Date(Typecho_Date::gmtTime());
return $now-&gt;timeStamp - $from < 246060 ? true : false;
}

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

最新文章调用

<?php $this->widget('Widget_Contents_Post_Recent')->to($post); ?>
<?php while($post->next()): ?>
<a href=”<?php $post->permalink(); ?>” title=”<?php $post->title(); ?>”>
<?php $post->title(25, '…'); ?></a>
<?php endwhile; ?>

热门文章调用

function getHotComments($limit = 10){
    $db = Typecho_Db::get();
    $result = $db->fetchAll($db->select()->from('table.contents')
        ->where('status = ?','publish')
        ->where('type = ?', 'post')
        ->where('created <= unix_timestamp(now())', 'post') //添加这一句避免未达到时间的文章提前曝光
        ->limit($limit)
        ->order('commentsNum', Typecho_Db::SORT_DESC)
    );
    if($result){
        foreach($result as $val){            
            $val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
            $post_title = htmlspecialchars($val['title']);
            $permalink = $val['permalink'];
            echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';        
        }
    }
}

大部分代码来自:https://www.itbulu.com/
https://maxwuhan.com/

vpsfree.fr 提供免费IPV6的VPS申请
« 上一篇 09-29
使用CSS3实现鼠标移到图片上图片放大
下一篇 » 10-13

评论 (1)

插入图片
  1. 头像
    未命名 Lv.1   河北省沧州市
    Windows 7 · FireFox
    沙发

    顺便逛逛居然看到了我以前的域名maxwuhan,博客圈真小,哈哈

    回复