typecho上一篇下一篇内获取图片封面实例

常规来说:主要记录上一篇/下一篇的代码

  1. /**
  2. * 显示下一篇
  3. *
  4. * @access public
  5. * @param string $default 如果没有下一篇,显示的默认文字
  6. * @return void
  7. */
  8. function theNext($widget, $default = NULL)
  9. {
  10. $db = Typecho_Db::get();
  11. $sql = $db->select()->from('table.contents')
  12. ->where('table.contents.created > ?', $widget->created)
  13. ->where('table.contents.status = ?', 'publish')
  14. ->where('table.contents.type = ?', $widget->type)
  15. ->where('table.contents.password IS NULL')
  16. ->order('table.contents.created', Typecho_Db::SORT_ASC)
  17. ->limit(1);
  18. $content = $db->fetchRow($sql);
  19. if ($content) {
  20. $content = $widget->filter($content);
  21. $link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">下一篇</a>';
  22. echo $link;
  23. } else {
  24. echo $default;
  25. }
  26. }
  27. /**
  28. * 显示上一篇
  29. *
  30. * @access public
  31. * @param string $default 如果没有下一篇,显示的默认文字
  32. * @return void
  33. */
  34. function thePrev($widget, $default = NULL)
  35. {
  36. $db = Typecho_Db::get();
  37. $sql = $db->select()->from('table.contents')
  38. ->where('table.contents.created < ?', $widget->created)
  39. ->where('table.contents.status = ?', 'publish')
  40. ->where('table.contents.type = ?', $widget->type)
  41. ->where('table.contents.password IS NULL')
  42. ->order('table.contents.created', Typecho_Db::SORT_DESC)
  43. ->limit(1);
  44. $content = $db->fetchRow($sql);
  45. if ($content) {
  46. $content = $widget->filter($content);
  47. $link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">上一篇</a>';
  48. echo $link;
  49. } else {
  50. echo $default;
  51. }
  52. }

将以上代码写入functions.php

调用代码如下:

  1. <?php thePrev($this); ?><?php theNext($this); ?>

typecho上一篇下一篇内获取图片封面实例,直接上代码参考把

  1. /**
  2. * 显示下一篇
  3. *
  4. * @access public
  5. * @param string $default 如果没有下一篇,显示的默认文字
  6. * @return void
  7. */
  8. function theNext($widget, $default = NULL)
  9. {
  10. $db = Typecho_Db::get();
  11. $sql = $db->select()->from('table.contents')
  12. ->where('table.contents.created > ?', $widget->created)
  13. ->where('table.contents.status = ?', 'publish')
  14. ->where('table.contents.type = ?', $widget->type)
  15. ->where('table.contents.password IS NULL')
  16. ->order('table.contents.created', Typecho_Db::SORT_ASC)
  17. ->limit(1);
  18. $content = $db->fetchRow($sql);
  19. if ($content) {
  20. $img = $db->fetchAll($db->select()->from('table.fields')->where('name = ? AND cid = ?','img',$result['cid']));
  21. if(count($img) !=0){
  22. //var_dump($img);
  23. $img=$img['0']['str_value'];
  24. if($img){}
  25. else{
  26. $img="/usr/themes/spimes/images/thumbs/other_thumbnail.png";
  27. }
  28. }
  29. // var_dump($img);
  30. // if($img == ""){
  31. // $img = "wu";
  32. // }
  33. $content = $widget->filter($content);
  34. $link = '<div class="entry-page-next j-lazy" style="background-image: url(' . $img . ')"><a href="' . $content['permalink'] . '"><span>' . $content['title'] . '</span> </a><div class="entry-page-info"> <span class="pull-right">下一篇 »</span></div></div>';
  35. echo $link;
  36. } else {
  37. echo $default;
  38. }
  39. }

如果想获取时间的话,这里就需要注意

评论时间就是$content['created'],这是unix时间戳,转换成人类看得懂的时间

  1. date('Y-m-d H:i:s', $content['created_at'])
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/76
0 评论
3.8k

发表评论

!