0%

wordpress文章转Git

wordpress文章转Git

最近把wordpress文章转到Git有些格式还是有问题有时间在整理吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$dbms = 'mysql';     //数据库类型
$host = 'localhost'; //数据库主机名
$dbName = 'ioqx'; //使用的数据库
$user = 'root'; //数据库连接用户名
$pass = 'root'; //对应的密码
$dsn = "$dbms:host=$host;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
echo "连接成功\n";
$sql = "SELECT wp.id,wp.post_parent,wp.post_date,wp.post_modified,wp.post_title,wp.post_content,wt.`name` FROM wp_posts wp LEFT JOIN wp_term_relationships wtr ON wp.id=wtr.object_id LEFT JOIN wp_terms wt ON wtr.term_taxonomy_id=term_id WHERE wp.post_status='publish' AND wt.`name` != ''"; // limit 2
foreach ($dbh->query($sql) as $row) {
echo "生成id:{$row['id']}\n";
$title =trim($row['post_title']) ;
$startDate = $row['post_date'];
$modifiedDate = $row['post_modified'];
$tags = $row['name'];
$content = $row['post_content'];

$md = <<<EOF
---
title: $title
date: $startDate
updated: $modifiedDate
categories: $tags
tags: $tags
---
$content
EOF;
file_put_contents('/Users/zhangzecheng/code/github/myBlog/source/_posts/' . $title . '.md', $md);
}
$dbh = null;
echo '已生成完毕';
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "\n");
}
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!