WordPress教程:另类方法解决WordPress文章ID不连续问题
WordPress默认情况下发布的文章ID不是连续的,因为自动草稿、文章修订版、页面、菜单、媒体等功能都要占用ID,所以使得ID不连续。百度了一圈,居然有人给出这样的解决方案:“这样如果你只是单纯发文章,不发页面,不添加菜单,不上传媒体的话,基本上此后的文章ID是连续的”,我也是相当无语。
我给出的方案很粗暴,很直接,就是占着茅坑不拉屎。建站规划的时候,就直接生成2000篇文章,保证ID的连续性。
为此,整个过程分为3个步骤:
➤创建Excel包含所有所需的占位文章。
➤将excel导入MySQL表。
➤把步骤2中的MySQL表导入WordPress post表。
创建一个Excel包含所有所需的帖子。
首先我们来创建一个包含2列的excel:
文章标题 文章内容
然后把整个execl文件另存为CVS逗号分隔符文件。
现在我们就有了近2000篇文章,只要想办法导入到wordpress里面就行了。
将excel导入MySQL
在wordpress数据库中创建一个MySQL表:一个包含2个字段的表'allpost'
登录mysql,执行以下语句:
CREATE TABLE `wordpress`.`allpost` ( `post_title` VARCHAR(50) NOT NULL , `post_content` VARCHAR(200) NOT NULL ) ENGINE = InnoDB;
删除测试页面和自动草稿。
DELETE FROM `wp_posts` WHERE `wp_posts`.`ID` = 2;
DELETE FROM `wp_posts` WHERE `wp_posts`.`ID` = 3;
重启一下mysql,让其释放ID。
使用phpmyadmin导入Excel到MySQL
浏览上传CVS文件后,其他的选项都不要动,只要注意“字段分隔符”那里是一个英文的逗号就行了。然后点击执行。
此时,我们已经把2000篇文章导入到allpost表。现在要做的就是利用wordpress自带的函数wp_insert_post把文章写入wp_posts表。
这个函数的用法:
$thepost = array(
'post_title' =>; 'Post title',
'post_content' =>; 'Post content',
'post_status' =>; 'publish',
'post_author' =>; 1,
'post_category' =>; array(1,2)
);
wp_insert_post( $thepost );
这里我们要注意的是post_status,文章状态不能是已发布(publish),得是草稿状态(draft)。
由于我们需要从MySQL表创建所有的文章,我们需要使用一个while循环,这样我们创建一个createpost.php,代码如下:
<?php
//the line below is connect to the wordpress database
require('./wp-blog-header.php');
$con = mysql_connect("localhost","root","textpass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//select all from table allpost and loop through one by one record
$result = mysql_query("SELECT * FROM wordpress.allpost",$con) or die(mysql_error());
while($line = mysql_fetch_array( $result )) {
//create post
$thepost = array(
'post_title' => $line['post_title'],
'post_content' => $line['post_content'],
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(1,2)
);
wp_insert_post( $thepost );
}
?>
上述代码只适用于PHP5及以下的版本,因为PHP7里面已经没有mysql_connect等函数。
若你用的是PHP7,可以使用下面的代码:
<?php
//the line below is connect to the wordpress database
require('./wp-blog-header.php');
$con = mysqli_connect("localhost","root","textpass");//root改为具体数据库用户名,textpass改为具体密码
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
//select all from table allpost and loop through one by one record
$result = mysqli_query($con, "SELECT * FROM wordpress.allpost") or die(mysqli_error($con)); //wordpress改为具体数据库名字
while($line = mysqli_fetch_array( $result )) {
//create post
$thepost = array(
'post_title' => $line['post_title'],
'post_content' => $line['post_content'],
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(1,2)
);
wp_insert_post( $thepost );
}
?>
把这个createpost.php放到wordpress网站根目录,chmod +x createpost.php后,/usr/local/php/bin/php createpost.php,稍等一小会,大功告成。
确认文章ID情况
登录mysql,use到博客数据库,执行如下命令,id和GUID一一对应为成功。
mysql> SELECT id,GUID FROM `bk_posts` where id<10\G
*************************** 1. row ***************************
id: 1
GUID: https://hqidi.com/?p=1
*************************** 2. row ***************************
id: 2
GUID: https://hqidi.com/?p=2
*************************** 3. row ***************************
id: 3
GUID: https://hqidi.com/?p=3
*************************** 4. row ***************************
id: 4
GUID: https://hqidi.com/?p=4
*************************** 5. row ***************************
id: 5
GUID: https://hqidi.com/?p=5
*************************** 6. row ***************************
id: 6
GUID: https://hqidi.com/?p=6
*************************** 7. row ***************************
id: 7
GUID: https://hqidi.com/?p=7
*************************** 8. row ***************************
id: 8
GUID: https://hqidi.com/?p=8
当然,也可以在phpmyadmin界面查看:
此时,如果有人要问,这么多文章,真正要写文章的时候,在所有文章页面翻页也是个麻烦事。
人是活的,比方说,你文章写到第666篇了,登录后台后,你直接访问 https://hqidi.com/666.html
最后懒人福利,提供做好的数据库文件下载:https://pan.lanzou.com/1339988
数据库超级干净,除了占位文章没有其他东西,数据库对应的WP后台管理账号信息:
用户名:huangqidi
密码 :zheshiyigehenqiangdemima
下载好我提供的数据库后,然后用Notepad++打开sql文件,把我的域名hqidi.com替换全部替换成你自己的域名,然后在mysql里新建一个wordpress库,导入即可。
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> use wordpress;
Database changed
mysql> source /root/wordpress.sql;
最近,记得新建一个用于wordpress程序连接数据库的用户。
评论前必须登录!
立即登录 注册