我希望将我所有的帖子导出为单独的纯文本文件。因此,格式可能类似于:
title.txt
nbsp;Title: title
Pub date: date
Category: cat
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
这可能吗?是否有插件或解决方法可以做到这一点?谢谢
我希望将我所有的帖子导出为单独的纯文本文件。因此,格式可能类似于:
title.txt
nbsp;Title: title
Pub date: date
Category: cat
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
这可能吗?是否有插件或解决方法可以做到这一点?谢谢
尝试此操作(您可能需要通过加载来引导WPwp-load.php
, 取决于您将此代码放在何处)。
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
//\'posts_per_page\' => -1 //uncomment this to get all posts
);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
$f = fopen(get_the_title() . \'.txt\', \'w\');
$content = \'Title: \' . get_the_title() . PHP_EOL;
$content .= \'Pub date: \' . get_the_date() . PHP_EOL;
$content .= \'Category: \';
foreach (get_the_category() as $cat) {
$content .= $cat->cat_name . \', \';
}
$content .= PHP_EOL . PHP_EOL;
$content .= get_the_content();
fwrite($f, $content);
fclose($f);
endwhile;
请记住,如果两篇文章的标题相同,则只能得到一个文本文件。粗略搜索不会找到任何这样做的插件。。。但是您可以使用内置的exporter作为构建自己插件的示例。它位于/wp-admin/includes/export.php
.
本质上,它是一个PHP页面,查询数据库以获取所有帖子,然后将内容转储到预构建的XML模板中,稍后可以导入该模板。
看起来很像你想切换到一个静态的博客生成器,比如Jekyll。以下答案基于pdb的答案,应该可以随时使用。我的回答可以处理:
多篇标题相同的帖子,请看:
<?
require_once(\'wp-load.php\');
function getFeaturedImage($postid) {
$featured_image = "";
$array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' );
if (count($array)>1) {
$featured_image = $array[0];
$featured_image = str_replace(\'http://martin-thoma.com/wp-content/uploads/\', \'\', $featured_image);
}
return $featured_image;
}
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'draft\',
\'posts_per_page\' => -1 //uncomment this to get all posts
);
$query = new WP_Query($args);
$i=0;
$titles = array();
while ( $query->have_posts() ) : $query->the_post();
$i++;
$name = $post->post_name;
if ($name == "") {
$name = strtolower(get_the_title());
$replacements = array(
\' \' => \'-\',
\'#\' => \'nr\',
\'/\' => \'or\',
\'ö\' => \'o\',
\'ü\' => \'u\',
\'ä\' => \'a\',
\'ß\' => \'ss\'
);
$name = str_replace(array_keys($replacements), $replacements, $name);
}
if (array_key_exists($name, $titles)) {
$name = $post->post_name."-".$post->ID;
} else {
$titles[$name] = true;
}
echo $name."<br/>";
$f = fopen("export/".$name. \'.md\', \'w\');
$content = "---".PHP_EOL;
$content.= "layout: post".PHP_EOL;
$content.= "title: ".get_the_title().PHP_EOL;
$content.= "author: ".get_the_author().PHP_EOL;
$content.= "date: ".get_the_date(\'Y-m-d h:i:s\').PHP_EOL;
$content.= "categories: ".PHP_EOL;
$cats = get_the_category();
if ($cats) {
foreach($cats as $category) {
$content .= \'- \'.$category->name.PHP_EOL;
}
}
$content .= \'tags: \';
$posttags = get_the_tags();
if ($posttags) {
$content .= PHP_EOL;
foreach($posttags as $tag) {
$content.= "- ".$tag->name.PHP_EOL;
}
} else {
$content .= "[]".PHP_EOL;
}
echo wp_get_attachment_image_src(get_the_post_thumbnail($post->ID));
$content .= \'featured_image: \'.getFeaturedImage($postid).PHP_EOL;
$content .= \'---\'.PHP_EOL;
$content .= get_the_content();
fwrite($f, $content);
fclose($f);
endwhile;
echo $i." posts exported."
?>
“为什么不使用默认的XML导出”
我没有答案,但OP可能想这样做,这样他或她就可以import the posts into Devonthink Pro 或类似的程序。
我发现通过添加以下功能(代替使用单独的插件),调整WordPress中的一些文本非常容易:add_filter( \'gettext\', \'of_site_translations\', 9999, 2 ); function of_site_translations( $translation, $text ) { if ( $text == \'Posts\' ) return \'News Posts\'; return $translation;