一般来说,我会采用查询帖子附件的方法,但保留作为帖子缩略图的附件。WP提供了一种使用get_post_thumbnail_id
(Codex Ref). 要修改另一篇文章中的代码,我将向$args
阻止查询缩略图附件的数组:
\'post__not_in\' => array(
get_post_thumbnail_id($post->ID)
)
The
post__not_in
参数将“指定不检索的帖子”(
Codex Ref)
要将整个内容放在一起,代码如下所示:
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => null,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'post__not_in\' => array(
get_post_thumbnail_id($post->ID)
)
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo apply_filters(\'the_title\', $attachment->post_title);
the_attachment_link($attachment->ID, false);
}
}
为了进一步优化您的查询,我强烈建议您探索WP\\U查询类(
Codex Ref). 它的威力只有在易用性上才能与之匹敌。