如何在首页和发现中插入自定义文章

jat
jat 这家伙很懒,还没有设置简介

8 人点赞了该文章 · 5379 浏览

首先,你要自己发表一篇文章,记下它的 id,比如这篇文章的 id 就是 134。 然后,在 app/home/main.php(首页)第 64 - 67 行 {{{ if (! $this->user_info['email']) { HTTP::redirect('/account/complete_profile/'); } }}} 或 app/explore/main.php(发现)第 47 - 57 行 {{{ if ($_GET['category']) { if (is_numeric($_GET['category'])) { $category_info = $this->model('system')->get_category_info($_GET['category']); } else { $category_info = $this->model('system')->get_category_info_by_url_token($_GET['category']); } } }}} 之后加入 {{{ $article_showed_id = 刚才记下的文章 id; $article_showed_info = $this->model('article')->get_article_info_by_id($article_showed_id); if ($article_showed_info['has_attach']) { $article_showed_info['attachs'] = $this->model('publish')->get_attach('article', $article_showed_info['id'], 'min'); $article_showed_info['attachs_ids'] = FORMAT::parse_attachs($article_showed_info['message'], true); } $article_showed_info['message'] = FORMAT::parse_attachs(nl2br(FORMAT::parse_markdown($article_showed_info['message']))); TPL::assign('article_showed_info', $article_showed_info); }}} $article_showed_info 是一个多维数组,可以在模板文件(views/default/home/index.tpl.htm 或 views/default/explore/index.tpl.htm)中使用 $this->article_showed_info 引用。 下面介绍下该数组的键: {{{ $this->article_showed_info['id'] 文章 id $this->article_showed_info['uid'] 发表这篇文章的用户 id $this->article_showed_info['title'] 文章标题(如果过长则为部分) $this->article_showed_info['message'] 文章内容 $this->article_showed_info['comments'] 文章的评论数 $this->article_showed_info['views'] 文章的浏览数 $this->article_showed_info['add_time'] 文章的添加时间(UNIX 时间) $this->article_showed_info['has_attach'] 是否有附件 $this->article_showed_info['lock'] 是否锁定 $this->article_showed_info['votes'] 被赞数 $this->article_showed_info['title_fulltext'] 文章标题(全部) $this->article_showed_info['category_id'] 文章所属话题的 id $this->article_showed_info['is_recommend'] 是否被推荐 $this->article_showed_info['attachs'] 文章的附件(关联数组,键为附件的 id,值为关联数组,请使用 foreach 遍历) $this->article_showed_info['attachs_ids'] 插入到文章的附件(数值数组,值为附件的 id) }}} 遍历 $this->article_info['attachs']: {{{ foreach ($this->article_info['attachs'] as $attach) { } }}} 下面介绍下 $attach 的键: {{{ $attach['id'] = 附件 id $attach['is_image'] 附件是否是图片 $attach['file_name'] 附件上传时的文件名 $attach['access_key'] 附件的访问密钥(暂时没用) $attach['attachment'] 附件的 url $attach['thumb'] 附件为图像时的缩略图 url(非图像时无此键) }}} 输出附件下载链接时,应该使用 download_url($attach['file_name'], $attach['attachment']),而不应该直接使用 $attach['attachment']),直接使用会导致文件名不正确。 如果想输出文章的更多内容可以参考 app/article/main.php,具体示例请看 views/default/article/index.tpl.htm。

发布于 2014-05-11 15:16

免责声明:

本文由 jat 原创发布于 WeCenter ,著作权归作者所有。

登录一下,更多精彩内容等你发现,贡献精彩回答,参与评论互动

登录! 还没有账号?去注册

jat
2014-05-11 15:58
图片缩略图可以调,但文章摘要和表格等不可以,前者数据库中没有,当然你也可以截取文章的部分内容做摘要;后者编辑器不支持。$article_showed_info 是通过 models/article.php 第 23 行 get_article_info_by_id 函数获取的。
accouter
2014-05-11 15:44
太好了!是不是意味着可以通过这个数组在发现列表或者文章列表中自定义文章输出的内容(比如可以调用出来文章的摘要和图片缩略图)了?之前的文章列表都是全文输出文章内容,然后用css控制显示,很多列表格式就实现不了,是不是意味着现在按照上述方法就可以像wordpress那种可以自定义文章列表的样式了?多谢!