我写了一个函数来获取post图像附件,有没有办法对这些图像进行排序,以便第一个始终是post\\u缩略图?
以下代码
谢谢
约翰
function oliver_single_gallery_thumbs() {
global $post;
$args = array(
\'post_parent\' => $post->ID, // For the current post
\'post_type\' => \'attachment\', // Get all post attachments
\'post_mime_type\' => \'image\', // Only grab images
\'order\' => \'ASC\', // List in ascending order
\'orderby\' => \'menu_order\', // List them in their menu order
\'numberposts\' => -1, // Show all attachments
\'post_status\' => null, // For any post status
);
$attachments = get_posts($args);
if ($attachments) {
$count = 0;
$the_thumbs = \'<ul id="single-thumbs">\';
foreach ($attachments as $attachment) {
$ze_count = $count+1;
$the_thumbs .= \'<li id="single-thumb-\'.$ze_count.\'">\';
if ($count==0) {
$thumb_attr = array(
\'class\' => "image-radius selected",
);
} else {
$thumb_attr = array(
\'class\' => "image-radius",
);
}
$the_thumbs .= wp_get_attachment_image($attachment->ID, \'single-thumb\', false, $thumb_attr);
$the_thumbs .= \'</li>\'."\\n";
$count = $count + 1;
}
$the_thumbs .= \'</ul>\';
}
echo $the_thumbs;
}
最合适的回答,由SO网友:tfrommen 整理而成
首先,从查询中排除特征图像(=帖子缩略图),然后将帖子数组设置为特征图像和其他图像的组合。
把这个直接放在下面$args = ...
及以上$attachments = ...
:
if (has_post_thumbnail($post->ID)) {
$featured_image = get_post_thumbnail_id($post->ID);
$args[\'exclude\'] = $featured_image;
$attachments = array(get_post($featured_image)) + get_posts($args);
} else