我正在寻找一种从媒体图像库中的每个图像创建帖子的好方法。我上传了大约200张图片,想为每张图片创建一篇帖子,我将图片设置为缩略图。
我正在使用照片主题,只想显示一些图片(标题和其他不需要的东西)。
非常感谢!!
我正在寻找一种从媒体图像库中的每个图像创建帖子的好方法。我上传了大约200张图片,想为每张图片创建一篇帖子,我将图片设置为缩略图。
我正在使用照片主题,只想显示一些图片(标题和其他不需要的东西)。
非常感谢!!
如果您的媒体库中有图像,您可以通过它们循环并创建post viawp_insert_post
.
function import_post_from_imgs() {
$images = get_posts(\'post_type=attachment&post_status=inherit&posts_per_page=-1\');
// just a minimal security check
if ( ! current_user_can(\'publish_posts\') ) return;
if ( ! empty($images) ) { foreach ( $images as $image) {
// prevent duplicate if for some reason the function is called more than once
if ( get_post_meta($image->ID, \'_imported\', true) ) continue;
$post = array(
\'post_title\' => $image->post_title,
\'post_content\' => \'\',
\'post_content\' => \'publish\'
);
// insert post
$postid = wp_insert_post( $post );
if ( $postid ) {
// set the image as thumbnalil
set_post_thumbnail($postid, $image->ID);
update_post_meta($image->ID, \'_imported\', 1);
}
} }
}
function go_import_post_from_imgs() {
if ( isset($_GET[\'import_images\']) ) import_post_from_imgs();
}
add_action(\'admin_init\', \'go_import_post_from_imgs\');
在上面的代码中,当$_GET
已设置变量“import\\u images”。因此,您必须登录到仪表板,然后页面的url如下所示http://example.com/wp-admin/
. 现在JU手动添加\'?import\\u images=1’,因此您的url成为http://example.com/wp-admin/?import_images=1
然后按Enter键。
几秒钟后,您应该会看到从图像创建的帖子。
请注意,此功能可从您更新的所有图像中创建帖子。如果要排除某些图像,可以采取两种方法:
查找要排除的图像的ID并添加这两行:
$exclude = array(12, 256, 587); // the ids you want to skip
if ( in_array($image->ID, $exclude) ) continue;
之前if ( get_post_meta($image->ID, \'_imported\', true) ) continue;
如果要排除少量图像,前面的方法很好,如果要排除更多图像,可以register a custom taxonomy 对于附件,请为要跳过的图像指定特定术语(例如“跳过”)(对于这些任务,请阅读here 可以帮助您)。之后,假设您的分类法被称为“媒体标签”,并且您已将“跳过”术语添加到要跳过的图像中,请在同一位置添加此行:
if ( has_term(\'skip\', \'media-tag\', $image->ID) ) continue;
我希望有人能阐明我试图解决的这个问题,即使只是简单地改变我的思维过程,告诉我如何解决这个问题。我正在将一个基于CakePHP的定制CMS web应用程序导入WordPress,方法是遍历数据库,进行大量SQL查询,构建数组,最后输出必要的XML文件,以便使用WordPress导入工具导入。目前我已经很好地导入了类别、标签和帖子/页面,没有问题,但问题似乎出在图像上。基于CakePHP的CMS中当前的图像格式是images/year/month/day/post_id/image_name.ext据我所知,