基本上,每次我发布一篇文章时,我都希望将wordpress数据库中的随机图像设置为该文章的特征图像。有人能帮我一下吗!
在提交帖子时随机设置WordPress数据库中的特色图片
1 个回复
最合适的回答,由SO网友:GhostToast 整理而成
Here is a way:
// listen for post being published
add_action(\'publish_post\', \'dreis_random_featured_image\');
function dreis_random_featured_image() {
global $post;
// find one random image
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => 1,
\'orderby\' => \'rand\'
);
$random_image = new WP_Query($args);
foreach($random_image as $image){
// set as thumbnail
set_post_thumbnail($post->ID, $image->ID);
}
// reset loop to avoid weirdness
wp_reset_query();
}
结束