MENU

解决 Typecho 文章 cid 不连续

• December 8, 2020 • 技术教程

Typecho 下文章编号(cid)不连续,虽然不影响什么,也无关紧要,但是对于有强迫症的人(比如我)来说,真的是无法忍受。还好有大佬提供了解决办法。

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

特别提醒:请在 PHP7 以下版本的服务器上执行。本操作涉及数据库,请提前做好备份工作。

解决 cid 不连续的问题
Tips 文章 cid 重新排列后,上传的附件所属文章可能不正确,需手动修改。

copy

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

原文链接:https://blog.jichun29.cn/438.html

- - - The END - - -
  • 文章标题:解决 Typecho 文章 cid 不连续
  • 文章链接:http://blog.uiuweb.cn/index.php/archives/12251.html
  • 版权所有:本文版权归 刘一彪 所有,转载请注明出处!除特殊注明外 (如有侵权,请 点此联系我 )
  • Archives QR Code Tip
    QR Code for this page
    Tipping QR Code