【发布时间】:2014-08-01 00:28:37
【问题描述】:
这是我的情况:
我正在重新设计现有的 Wordpress 网站。
新设计将所有图片与实际帖子内容分开,并将其放入Featured Gallery。
目前,每个帖子的帖子内容中都有您典型的内嵌图片。
这是否甚至可以远程从帖子内容中提取所有内嵌图像,并使用这些图像为每个帖子创建特色画廊?
过去我做了类似下面的事情来抓取帖子内容中的第一张图片,并将其设置为标准的特色图片,但与我在这个困境中所要做的完全不同。
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
} //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
【问题讨论】: