前言
这篇文章目的是解决Typecho下文章cid、分类和标签mid不连续的问题,对于某些强迫症博主而言mid,cid不连续是看不过去的。

因为typech的机制:页面,附件,都要占cid的。而且点击一次创建文章cid就会增加一次,即使你没有书写内容,那个cid也被认为已
经使用了,所以下一次生成的cid就会跳过那个,造成不连续。

特别提醒
尽量请在PHP7.0以下版本的服务器上执行。本操作涉及数据库,请提前做好备份工作。基本没啥问题,一般出现在设置了头图地址(thumb)的童鞋才会出现文章首页混乱不匹配,需重新配置下,经过第一次排序后接下来不会有该问题,有问题也是未排序文章。

分类和标签 mid 重新排列后,子分类所属父分类可能不正确,需手动修改,如无二级分类,可略过。

解决方案
在服务器上新建 cid.php 和 mid.php 并修改代码中的数据库对应的数据,并在浏览器地访问一下即可食用

cid.php

<?php
/**
 * Typecho重新排列不连续的文章ID
 */

$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';
?>

mid.php

<?php
/**
 * Typecho重新排列分类和标签(meta)不连续的mid
 */

$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($mid)
{
    global $no; 

    // 修改meta id,并修改分类、标签、自定义字段、评论的对应关系
    $sql = 'update typecho_metas set mid = ' . $no . ' where mid = ' . $mid;
    mysql_query($sql);
    $sql = 'update typecho_relationships set mid = ' . $no . ' where mid = ' . $mid;
    mysql_query($sql);

    $no = $no + 1;
}

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

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

// 重新设置meta id自增起点
mysql_query('alter table typecho_metas AUTO_INCREMENT = ' . $no);
echo 'ok';
?>