我希望用户头像图像设置为特征图像,如果没有特征图像。
我正试着这样做。
function auto_featured_image() {
global $post;
if (!has_post_thumbnail($post->ID)) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
//$attached_avatar = get_avatar( get_the_author_meta( \'ID\' ));
$attached_avatar = get_avatar(1);
if ($attached_image) { // set feature image as the 1st image on post.
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}/*elseif($attached_avatar){ // set feature image as the avatar image.
foreach ($attached_avatar as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}*/
}
}
// Use it temporary to generate all featured images
add_action(\'the_post\', \'auto_featured_image\');
// Used for new posts
add_action(\'save_post\', \'auto_featured_image\');
add_action(\'draft_to_publish\', \'auto_featured_image\');
add_action(\'new_to_publish\', \'auto_featured_image\');
add_action(\'pending_to_publish\', \'auto_featured_image\');
add_action(\'future_to_publish\', \'auto_featured_image\');
将特征图像设置为post works fine上的第一个图像。但将头像设置为功能图像不起作用。
我怎样才能让它工作?谢谢
最合适的回答,由SO网友:Mark Kaplun 整理而成
一般来说,头像不能替代附件,除非您有一个插件,该插件强制通过wordpress上传过程将所有头像上传到站点(或者换句话说,包括在媒体库中)。头像可以是任何URL,而附件在您的DB中有id。因此,您将化身视为附件的尝试不会成功。
唯一可行的方法是,您首先从其所在的任何服务中获取头像图像,并从中创建附件。然后,您可以按照代码所暗示的方式来处理它。