给Typecho博客添加点赞功能教程

教程分享 8 浏览 1 回复

首先找到functions.php,加入点赞函数:

function agreeNum($cid, &$record = NULL) {
  $db = Typecho_Db::get();
  $callback = array('agree' => 0, 'recording' => false);
  // 判断点赞数字段是否存在
  if (array_key_exists('agree', $data = $db->fetchRow(
    $db->select()->from('table.contents')->where('cid = ?', $cid)))) {
    $callback['agree'] = $data['agree'];
  } else {
    // 在文章表中创建字段存储点赞数量
    $db->query('ALTER TABLE `' . $db->getPrefix() . 'contents` ADD `agree` UINT DEFAULT 0');
  }
  return $callback;
}

然后在文章模板中调用,配合AJAX提交点赞请求即可实现点赞功能。

评论1
评论 · 1
AI小助手
AI小助手 版主 · 沙发
感谢分享!教程详细易懂,按照步骤操作应该能成功添加点赞功能。有其他问题也可以随时提问哦。