先来看下效果图,添加文章的时候随机356-999,也可以手动修改这个值,编辑的时候实际多少阅读量就显示多少阅读量,也可以在编辑的时候对这个值进行修改。
![图片[1]-Typecho实现给文章添加初始的阅读量,可随机或者指定-米克随笔](https://blog.mk4.cc/wp-content/uploads/2022/07/image-58.png)
1,修改视图
admin/wirte-post.php打开这个文件找到合适的位置,添加个input框,用来输出阅读参数,我是放在日期下方的。
<!--新增文章默认阅读量-->
<section class="typecho-post-option">
<label for="views" class="typecho-label"><?php _e('随机阅读数'); ?></label>
<p><input type="text" id="views" name="views" value="<?=$post->views?($post->views):rand(356,999)?>"></p>
<p class="description"><?php _e('默认自动生成,也可手动修改'); ?></p>
</section>
2,修改表单接收参数,编辑下面的文件
/var/Widget/Contents/Post/Edit.php
大概在719行,默认views字段为0,所以表单里面没有这个字段
原内容:
$contents = $this->request->from('password', 'allowComment','allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility');
修改为
$contents = $this->request->from('password', 'allowComment','allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility','views');
3,修改控制文章增删改查的文件
/var/Widget/Abstract/Contents.php
原内容:
public function select(){
return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
'table.contents.parent')->from('table.contents');
}
修改为
public function select(){
return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
'table.contents.parent','table.contents.views')->from('table.contents');
}
继续修改这个文件,在插入和更新字段的地方新增views,大概在288行和334行,增加如下内容,两个地方都要修改
'views'=> empty($content['views']) ? 0 : intval($content['views'])
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
- 最新
- 最热
只看作者