typecho获取指定栏目下的文章

typecho数据库里面,文章数据是单独一个表,而栏目和文章id的关联又是一个表,所以单独输出某一个栏目下的文章内容,则需要在输出文章的时候进行联合查询(SQL 中 JOIN 子句用于把来自两个或多个表的行结合起来)

参考代码如下:

  1. //直接在文章调用mou(分类id)
  2. function mou($cid=NULL){
  3. if(empty($cid)){
  4. $cid = 1;
  5. }
  6. $db = Typecho_Db::get();
  7. $query = $db->select()
  8. ->from('table.contents')
  9. ->join('table.relationships', 'table.contents.cid = table.relationships.cid',Typecho_Db::INNER_JOIN)
  10. ->where('table.contents.type = ?', 'post')->where('table.relationships.mid = ?', $cid);
  11. $arr = $db->fetchAll($query);
  12. return $arr;
  13. // print_r($arr) ;
  14. }

这样就可以输出指定栏目下的文章内容了

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1075
1 条评论
2.9k

发表评论

!