解决Typecho文章cid不连续的问题

插件开发及发布

版主: woniou

回复
kavico
帖子: 19
注册时间: 2017年 11月 10日 10:55
联系:

解决Typecho文章cid不连续的问题

帖子 kavico »

虽然说文章ID不连续并不影响什么,但或多或少的对博客作者心情会有点影响,我刚开始就觉得很纳闷,看着就很不爽。WORDPRESS有相应的解决方案,但是TEPECHO没有,经过一系列调整,基本完美,数据库操作前注意备份。

由于代码较多,您可以访问https://www.kavico.net/post/877.html
头像
ClayMore
帖子: 2481
注册时间: 2007年 11月 29日 02:55
来自: Sleeping Forest
联系:

Re: 解决Typecho文章cid不连续的问题

帖子 ClayMore »

将代码保存为php文件,上传至网站根目录,在浏览器地址栏访问一下即可。

特别提醒:请在PHP7以下版本的服务器上执行。本操作涉及数据库,请提前做好备份工作。另外,文章cid重新排列后,上传的附件所属文章可能不正确,需手动修改。

代码: 全选

<?php
/**
 * Typecho重新排列不连续的文章ID
 * Blog    :http://www.kavico.net/
 */


$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);

$no = 1;

function change_id($cid)
{
    global $no;
   
    // 修改post cid,并修改分类、标签、自定义字段、评论的对应关系
    $sql = 'update typecho_contents set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
    $sql = 'update typecho_relationships set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
    $sql = 'update typecho_comments set cid = ' . $no . ' where cid = ' . $cid;
    mysql_query($sql);
   
    $no = $no + 1;
}

mysql_select_db($database_blog, $blog);
$query_postRecord = "SELECT cid FROM typecho_contents ORDER BY cid ASC";
$all_postRecord = mysql_query($query_postRecord);
$row_postRecord = mysql_fetch_assoc($all_postRecord);

do {
    change_id( $row_postRecord['cid'] );   
} while ($row_postRecord = mysql_fetch_assoc($all_postRecord));

// 重新设置post id自增起点
mysql_query('alter table typecho_contents AUTO_INCREMENT = ' . $no);

echo 'ok';

?>
回复