运用typecho程序搭建博客,假如想在网站页面上的某个位置显现博主想要指定的几篇文章怎么办?不懂typecho开发的博主可能会挑选直接在模板文件里到场html代码完成,如许做明显不够天真,增添删除修正都异常不方便,而下面博客吧分享的函数代码,博主仅须要设置文章id即可挪用要显现的文章列表,异常便利。
操纵步骤:
1、把下面的代码增加到主题的functions.php文件:
123456789101112131415161718192021222324 | //by boke8.netfunction boke8GetIdPosts($id){if($id){$getid = explode(',',$id);$db = Typecho_Db::get();$result = $db->fetchAll($db->select()->from('table.contents')->where('status = ?','publish')->where('type = ?', 'post')->where('cid in ?',$getid)->order('cid', Typecho_Db::SORT_DESC));if($result){$i=1;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>';}}}else{echo '请设置要挪用的文章ID';}} |
2、在要显现文章位置对应的模板文件增加以下挪用代码:
1 | <?php boke8GetIdPosts('1,4,6');?> |
个中1,4,6是要挪用的文章id,修正为本身要挪用的文章id即可,多个id用英文逗号离隔。

评论列表