查询帖子的特征图像是这个大小


query posts whos featured image is this size

因此,我的网站上有两种类型的特色图像,横向和纵向。这些肖像的高度都大于650像素。没有标签、类别或任何其他wordpress定义的字段来区分这两者。我正试图找出一种方法,只使用php从每个方向单独查询随机帖子。

这是我目前所掌握的,这是错误的。我试图确定每个帖子缩略图的高度,将其设置为一个变量,然后将该变量附加到每个帖子中。然后,使用查询args数组中的post查询post。希望我走在正确的轨道上。

<?php
$pposts = get_posts(array('post_type' => 'portfolio'));
foreach ($pposts as $post) {
    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    list($width, $height, $type, $attr) = getimagesize($url);
        if ($height >= 650) {
        $orientation = 1; // Portrait
        }
        else {
        $orientation = 2; // Landscape
        };
    $post->orientation = $orientation;
    }
$arg1 = array('orientation' => 1, 'post_type' => 'portfolio', 'orderby' => 'rand', 'showposts' => 1); // Portrait query
$arg2 = array('orientation' => 2, 'post_type' => 'portfolio', 'orderby' => 'rand', 'showposts' => 1); // Landscape query
$query1 = new WP_Query($arg1);
if ( $query1->have_posts()  ) {
    while ( $query1->have_posts() ) {
        $query1->the_post();
        the_post_thumbnail(); 
    }
};
$query2 = new WP_Query($arg2);
if ( $query2->have_posts()  ) {
    while ( $query2->have_posts() ) {
        $query2->the_post();
        the_post_thumbnail(); 
    }
};
$query3 = new WP_Query($arg1);
if ( $query3->have_posts()  ) {
    while ( $query3->have_posts() ) {
        $query3->the_post();
        the_post_thumbnail(); 
    }
};
?>

您已经对所有post_type='portfolio'进行了1次循环。

所以您不需要secound查询。只需要为它构建一个函数,比如这样。

function get_posts_by_image_size($width, $height, $size = 'full') {
    global $post;
    $pposts = get_posts(array('post_type' => 'portfolio'));
    foreach ($pposts as $post) {
        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
        if($post_thumbnail_id) {
            $image = wp_get_attachment_image_src( $post_thumbnail_id, $size );
            if($image[1] >= $width && $image[2] >= $height) {
                the_title(); // here you print the data
            }
        }
    }
}
get_posts_by_image_size(0, 200); // Run the function whrere you want.

发布这篇文章是因为这是我的问题和我试图实现的目标的完整答案。好吧,所以我取了Shibi的代码,并将post-id附加到不同的数组中。然后,我从这些数组中随机选择了关键点,以便在调用后期缩略图时参考。最终的结果正是我想要的:1)随机的肖像,2)随机的风景,3)不同的随机肖像。

如果有人看到任何可以改进的地方,请告诉我。我没有在这段代码中包含我的div包装器,因为这只适用于我的情况。我还将原始查询限制为100个帖子,只是为了让一切运行得更快(可能会做一些测试,看看它的帖子数量会减慢多少)。

<?php
// Parameters as separate arguments
function get_posts_by_image_size($width, $height, $size = 'full') {
    global $post;
    $pposts = get_posts(array('post_type' => 'portfolio', 'numberposts'=> 100));
    foreach ($pposts as $post) {
        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
        if($post_thumbnail_id) {
            $image = wp_get_attachment_image_src( $post_thumbnail_id, $size );
            if($image[1] >= $width && $image[2] >= $height) {
            $portrait[] = get_the_ID();
            }
            elseif($image[1] >= $width && $image[2] <= $height) {
            $landscape[] = get_the_ID();        
        }
    }
}
$portrait_id = array_rand($portrait, 2);
$landscape_id = $landscape[rand(1, count($landscape))];
echo get_the_post_thumbnail( $portrait[$portrait_id[0]]);
echo get_the_post_thumbnail( $landscape_id);
echo get_the_post_thumbnail( $portrait[$portrait_id[1]]);
}
get_posts_by_image_size(0, 650); // Run the function whrere you want.

?>