检索图像附件首先获取发布的缩略图

时间:2013-04-25 作者:John

我写了一个函数来获取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;
}

1 个回复
最合适的回答,由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

结束

相关推荐

Upload images - Theme options

我正在使用这个很棒的[图坦卡门+教程][1]创建主题选项页面。这对我来说很好,因为我需要通过css更改div的背景图像。它可以很好地与一个图像,但我不知道如何使它与2个图像?如果你能告诉我我做错了什么,那将是一个巨大的帮助?谢谢 <?php function wptuts_get_default_options() { $options = array(); $options[] = array(\'logo\' => \'\');