Products
GG网络技术分享 2025-03-18 16:13 0
WordPress的特色图像是一个很实用的功能,可以在文章列表中为每篇文章添加一张缩略图。如果当前文章中没有图片,但又想显示一张默认的缩略图该怎么办?调用媒体库中某个图片作为默认的缩略图:
将下面的代码添加到当前主题的functions.php中:
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);
}
} else {
set_post_thumbnail($post->ID, '11');
}
}
} //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');上面代码中的数字11,是媒体库中某个图片附件的ID号。
WordPress网站在添加媒体库时出现无法显示图片的情况是什么?WordPress媒体库无法显示图片,问题可能有多种原因。您可能已经修改了WordPress配置文件wp-config.php或函数模板函数s.php,以便在保存文件的同时更改文件编码,从而导致媒体库显示错误。
Demand feedback