我想知道你是否在寻找与我最初回答的不同的东西。在重读了你的帖子后,我想如果我读对了,你的图片实际上是附件,而不是缩略图。如果这些附件仍然没有保存在db中,那么只有您正确指出的关于附件的信息
在这种情况下,如果操作不当,加载附件可能会非常昂贵。令人沮丧的是,WordPress不允许您在不传递post父ID的情况下直接查询附件。
要获取帖子的附加图像,您可以尝试以下方法,这样做会更快一些
/**
* Because we really just need post ID\'s and nothing more like
* custom fields, postdata, and post terms, we will not be updating
* the post cache. This saves a lot of time and resources. If you are
* going to need custom fields or post data or terms, then you should remove
* cache_results because this will cause huge increase in db queries when you
* try to get post terms or custom fields
*
* Also, we will only get post ID\'s as we do not need anything else
*/
$args = [
\'fields\' => \'ids\', // Get only post ID\'s
\'cache_results\' => false, // Do not update post caches
// Add any extra arguments
];
$q = get_posts( $args );
if ( $q ) {
foreach ( $q as $id ) {
/**
* Get all attached images
*
* We will be doing what get_attached_media does, but because we only
* have post ID\'s and have not updated the post cache, we will not be
* using get_attached_media() as it will lead to a large number of db hits
* because get_attached_media() will use get_post() to get the complete post object
*/
$image_args = [
\'post_parent\' => $id,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
];
$images = get_children( $image_args );
if ( !$images )
continue;
// Loop throught the images
foreach ( $images as $image ) {
// Output the images as needed
// For debugging, see the var_dump for values
var_dump( $image );
}
}
}
原始答案Post缩略图不会显式保存在db中,只有特定缩略图的ID会保存为Post meta
wp_postmeta
桌子特定的元键是
_thumbnail_id
.
您可以通过get_the_post_thumbnail()
作用也可以将字符串或自定义属性数组设置为第三个参数
值得注意的是,get_the_post_thumbnail()
不显示缩略图,只返回缩略图。在循环中,您可能希望使用the_post_thumbnail()
显示缩略图,在这种情况下,上述参数将分别为第一个和第二个参数。
在自定义循环中加载缩略图的成本很高,因为自定义查询不会缓存缩略图。只覆盖主查询。您需要在自定义循环中明确设置缩略图的缓存。
您可以尝试以下可能的解决方案:(NOTE: 所有内容都未经测试,可能有问题
(假设您想使用medium
调整缩略图大小,只显示缩略图,不显示其他内容,如果需要任何其他帖子属性,请删除过滤器
让我们先看看应该进入的过滤器functions.php
/**
* Custom filter to retrieve only post ID\'s.
*
* We cannot set the fields arguments to ID in our query because
* we need to update the thumbnail cache, and if you look at
* update_post_thumbnail_cache(), we need the post as an object
* so we can use $post->ID, if we set the fields parameter, we only
* get an array of ID\'s, not post objects with only ID\'s
*/
add_filter( \'posts_fields\', function ( $fields, \\WP_Query $q ) use ( &$wpdb )
{
remove_filter( current_filter(), __FUNCTION__ );
// Only target a query where the new wpse_fields parameter is set with a value of ID
if ( \'ID\' === $q->get( \'wpse_fields\' ) ) {
// Only get the post ID field to reduce server load
$fields = "$wpdb->posts.ID";
}
return $fields;
}, 10, 2);
这个过滤器将大幅度减少服务器负载,因为我们只获得post ID,但仍然维护post对象。
您的自定义查询可以如下所示:
/**
* Because we only need to display the post thumbnails, we will not be
* updating the term cache. If you need post term info, remove the
* update_post_term_cache argument. This will increase performance if you
* do not need post term info
*/
$args = [
\'meta_key\' => \'_thumbnail_id\', // Only get posts with thumbnails
\'wpse_fields\' => \'ID\', // Our custom argument to get only post ID\'s
\'update_post_term_cache\' => false, // Do not update the post caches to make the query faster
// Any other arguments
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
// Update the post thumbnail cache
update_post_thumbnail_cache( $q );
while ( $q->have_posts() ) {
$q->the_post();
// Display the post thumbnail, medium size
if ( has_post_thumbnail() ) // Not really necessary in current context
the_post_thumbnail( \'medium\' ); // Get medium size post thumbnail
}
wp_reset_postdata();
}
编辑
VERY IMPORTANT NOTE
非常不鼓励使用短PHP标记,实际上WordPress中不允许使用短PHP标记。这是一种非常糟糕的编码实践。您应该养成使用正确PHP标记的习惯(*
<?php
和
?>