增加阅读数,自定义排序,order值列表显示等
首先弄个插件增加阅读数和自定义order值,然后修改admin/manage-posts.php第99-113行,增加order值显示
修改如下:
<table class="typecho-list-table">
<colgroup>
<col width="20"/>
<col width="6%"/>
<col width="6%"/>
<col width="45%"/>
<col width=""/>
<col width="18%"/>
<col width="16%"/>
</colgroup>
<thead>
<tr>
<th> </th>
<th>order</th>
<th><?php _e('评论数'); ?></th>
<th><?php _e('标题'); ?></th>
<th><?php _e('作者'); ?></th>
<th><?php _e('分类'); ?></th>
<th><?php _e('日期'); ?></th>
</tr>
</thead>
<tbody>
<?php if($posts->have()): ?>
<?php while($posts->next()): ?>
<tr id="<?php $posts->theId(); ?>">
<td><input type="checkbox" value="<?php $posts->cid(); ?>" name="cid[]"/></td>
<td><?php $posts->order(); ?></td>
插件使用说明都写在里面了,还是记一下:
开启后,可以在发布编辑文章时,高级设置里编辑文章Order值,可以在调用时根据Order值排序,调用示例:
<?php $this->widget('Widget_Archive@index', 'order=order&asc=1&pageSize=6&type=category', 'mid=1')->to($categoryposts); ?>
说明:order=order,排序方式选择,可选值为viewsNum,commentsNum, order, cid, created, modified。此种调用方法需要制定分类,不然首页和分类页文章页显示不同。
asc=1即可以倒序排列,不设置就是正常排列。
暂时不会做插件,所以修改系统文件var/widget/archive.php第1386行左右往下为以下内容:
if (isset($this->parameter->order)) {
$order = $this->parameter->order;
}else {
$order = 'created';
}
/** 仅输出文章 */
$this->_countSql = clone $select;
$select->order('table.contents.'.$order, $this->parameter->asc ? Typecho_Db::SORT_ASC : Typecho_Db::SORT_DESC)
->page($this->_currentPage, $this->parameter->pageSize);
$this->query($select);
全局指定条件调用,使用Recent.php调用。方法:
<?php $this->widget('Widget_Contents_Post_Recent@aaa', 'order=order&pageSize=6')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
var/Widget/Contents/Post/Recent.php
文件修改为:
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 最新文章--增加制定条件调用
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
* @:<?php $this->widget('Widget_Contents_Post_Recent@aaa', 'order=order&pageSize=6')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
*/
class Widget_Contents_Post_Recent extends Widget_Abstract_Contents
{
/**
* 执行函数
*
* @access public
* @return void
*/
public function execute()
{
if (isset($this->parameter->order)) {
$order = $this->parameter->order;
}else {
$order = 'created';
}
$this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
$this->db->fetchAll($this->select()
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $this->options->time)
->where('table.contents.type = ?', 'post')
->order('table.contents.'.$order, $this->parameter->asc ? Typecho_Db::SORT_ASC : Typecho_Db::SORT_DESC)
->limit($this->parameter->pageSize), array($this, 'push'));
}
}
以上就是全部修改内容。
插件内容是这些:
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Sort By Order
*
* @package SortOrder
* @author qining
* @version 1.0.0
* @link http://typecho.org
*/
class SortOrder_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('admin/write-post.php')->advanceOption = array('SortOrder_Plugin', 'order');
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
// contents 表中若无 viewsNum 字段则添加
if (!array_key_exists('viewsNum', $db->fetchRow($db->select()->from('table.contents'))))
$db->query('ALTER TABLE `'. $prefix .'contents` ADD `viewsNum` INT(10) DEFAULT 0;');
//增加浏览数
Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('SortOrder_Plugin', 'viewCounter');
//把新增的字段添加到查询中
Typecho_Plugin::factory('Widget_Archive')->select = array('SortOrder_Plugin', 'selectHandle');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
$delFields = Typecho_Widget::widget('Widget_Options')->plugin('SortOrder')->delFields;
if($delFields){
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$db->query('ALTER TABLE `'. $prefix .'contents` DROP `viewsNum`;');
}
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$addorder = new Typecho_Widget_Helper_Form_Element_Radio('addorder',
array(1=>_t('打开'),0=>_t('关闭'),), '1', _t('Order值'),_t('开启后,可以在发布编辑文章时,高级设置里编辑文章Order值,可以在调用时根据Order值排序,<br>
示例:<?php $this->widget(\'Widget_Archive@index\', \'order=order&asc=1&pageSize=6&type=category\', \'mid=1\')->to($categoryposts); ?><br>
说明:order=order,排序方式选择,可选值为viewsNum,commentsNum, order, cid, created, modified。此种调用方法需要制定分类,不然首页和分类页文章页显示不同。<br>
asc=1即可以倒序排列,不设置就是正常排列。<br>
暂时不会做插件,所以修改系统文件var/widget/archive.php第1386行左右往下为以下内容<br><br>
if (isset($this->parameter->order)) {<br>
$order = $this->parameter->order;<br>
}else {<br>
$order = \'created\'; <br>
}<br>
/** 仅输出文章 */<br>
$this->_countSql = clone $select;<br>
$select->order(\'table.contents.\'.$order, $this->parameter->asc ? Typecho_Db::SORT_ASC : Typecho_Db::SORT_DESC)<br>
->page($this->_currentPage, $this->parameter->pageSize);<br>
$this->query($select);<br><br>
全局指定条件调用,使用Recent.php调用。方法:<code><?php $this->widget(\'Widget_Contents_Post_Recent@aaa\', \'order=order&pageSize=6\')->parse(\'<li><a href="{permalink}">{title}</a></li>\'); ?></code><br>
文件修改为:<br><br>
class Widget_Contents_Post_Recent extends Widget_Abstract_Contents<br>
{<br>
public function execute()<br>
{<br><br>
if (isset($this->parameter->order)) {<br>
$order = $this->parameter->order;<br>
}else {<br>
$order = \'created\'; <br>
}<br><br>
$this->parameter->setDefault(array(\'pageSize\' => $this->options->postsListSize));<br><br>
$this->db->fetchAll($this->select()<br>
->where(\'table.contents.status = ?\', \'publish\')<br>
->where(\'table.contents.created < ?\', $this->options->time)<br>
->where(\'table.contents.type = ?\', \'post\')<br>
->order(\'table.contents.\'.$order, $this->parameter->asc ? Typecho_Db::SORT_ASC : Typecho_Db::SORT_DESC)<br>
->limit($this->parameter->pageSize), array($this, \'push\'));<br>
}<br>
}<br>
'));
$form->addInput($addorder);
$delFields = new Typecho_Widget_Helper_Form_Element_Radio('delFields',
array(0=>_t('保留数据'),1=>_t('删除数据'),), '0', _t('卸载设置'),_t('文章查看数会在_contents表中增加viewsNum项纪录,卸载插件后是否需要保留数据。'));
$form->addInput($delFields);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function order()
{
$addorder = Typecho_Widget::widget('Widget_Options')->plugin('SortOrder')->addorder;
if($addorder){
$post = Typecho_Widget::widget('Widget_Contents_Post_Edit');
echo '<section class="typecho-post-option">
<label for="order" class="typecho-label">排序</label>
<p><input id="order" name="order" type="text" value="';
$post->order();
echo '" class="w-100 text" /></p><p class="description">文章order值,方便按固定值顺序排序,不需要则不填,默认为0。</p></section>';
}
}
/**
* 增加浏览量
* @params Widget_Archive $archive
* @return void
*/
public static function viewCounter($archive){
if($archive->is('single')){
$cid = $archive->cid;
$views = Typecho_Cookie::get('__post_views');
if(empty($views)){
$views = array();
}else{
$views = explode(',', $views);
}
if(!in_array($cid,$views)){
$db = Typecho_Db::get();
$row = $db->fetchRow($db->select('viewsNum')->from('table.contents')->where('cid = ?', $cid));
$db->query($db->update('table.contents')->rows(array('viewsNum' => (int)$row['viewsNum']+1))->where('cid = ?', $cid));
array_push($views, $cid);
$views = implode(',', $views);
Typecho_Cookie::set('__post_views', $views); //记录查看cookie
}
}
}
//cleanAttribute('fields')清除查询字段,select *
public static function selectHandle($archive){
$user = Typecho_Widget::widget('Widget_User');
if ('post' == $archive->parameter->type || 'page' == $archive->parameter->type) {
if ($user->hasLogin()) {
$select = $archive->select()->where('table.contents.status = ? OR table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)',
'publish', 'hidden', 'private', $user->uid);
} else {
$select = $archive->select()->where('table.contents.status = ? OR table.contents.status = ?',
'publish', 'hidden');
}
} else {
if ($user->hasLogin()) {
$select = $archive->select()->where('table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)', 'publish', 'private', $user->uid);
} else {
$select = $archive->select()->where('table.contents.status = ?', 'publish');
}
}
$select->where('table.contents.created < ?', Typecho_Date::gmtTime());
$select->cleanAttribute('fields');
return $select;
}
}
错误修正
有一个文件修改忘记写了
修改文件var/Widget/Contents/Post/Edit.php 719行为:
'allowPing', 'allowFeed', 'slug', 'tags', 'order', 'text', 'visibility');
上面的不修改,则order值无法提交到数据库
涉及修改文件已打包,这里下载吧
文件路径:admin/manage-posts.php
var/Widget/Archive.php
var/Widget/Contents/Post/Recent.php
usr/plugins/SortOrder/Plugin.php
var/Widget/Contents/Post/Edit.php
虽然现在不需要了,但是根据我现在的知识水平,做成次插件应该是轻而易举了。--2020.10.29 23:46
最后修订于 2020-10-29 23:48:18
- 上一篇 中国色
- 下一篇 2018年,新的开始