Typecho博客如何调用指定ID文章的方法代码

搬瓦工机场JMS

使用Typecho程序搭建博客,如果想在网站页面上的某个位置显示博主想要指定的几篇文章怎么办?不懂typecho开发的博主可能会选择直接在模板文件里加入html代码实现,这样做显然不够灵活,增加删除修改都非常不方便,而下面VPS全科网分享的函数代码,博主仅需要设置文章id即可调用要显示的文章列表,非常便捷。

操作步骤:

1、把下面的代码添加到主题的functiONs.php文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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用英文逗号隔开。

未经允许不得转载:搬瓦工VPS_美国VPS » Typecho博客如何调用指定ID文章的方法代码

赞 (0) 打赏

相关推荐

    暂无内容!

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏