admin管理员组文章数量:1303427
Basically every time that I publish a post I want that a random image from my wordpress database be set as the feature image for that post. Can someone help me with this please!
Basically every time that I publish a post I want that a random image from my wordpress database be set as the feature image for that post. Can someone help me with this please!
Share Improve this question asked Mar 14, 2013 at 16:32 David ReisDavid Reis 351 silver badge7 bronze badges1 Answer
Reset to default 1Here 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();
}
本文标签: pluginsSet featured image randomly from Wordpress Database on post submission
版权声明:本文标题:plugins - Set featured image randomly from Wordpress Database on post submission 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741731230a2394853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论