0

我想要组合时总是 900px 宽的图像行。

如果它的 1 个图像高度是可变的,宽度是 900px 2 个图像一个可能是 300px 宽,另一个是 600px 宽。

到目前为止,这是我的尝试。

    <?php
        $maxHeight = 400;//maximum height of a thumb
        $offset = 0;//start on recent post
        $maxRowWidth = 900;//maximum width of a row
        $stopId=20;//max of 20 posts 
        set_post_thumbnail_size( 9999, $maxHeight );
        $rowWidth=0;//initial row width
        $column = 0;    

        if ( have_posts() ) : while ( have_posts() ) : the_post();
            if ( has_post_thumbnail() ) {
                $tn_id = get_post_thumbnail_id( $post->ID );
                $img = wp_get_attachment_image_src( $tn_id, array(9999,$maxHeight) );
                $width = $img[1];//holds the default image width given max height
                $height = $img[2];//not used yet
                $column = $column+1;
                $rowWidth = $rowWidth+$width;
                if ($rowWidth > $maxRowWidth){
                    $aspectRatio = $rowWidth / $maxHeight;
                    $height= $maxRowWidth / $aspectRatio;
                    //I get an error here when I nest this loop wordpress doesn't like this
                    $query = 'showposts='.$column.'&offset='.$offset;

                    query_posts('$query');
                    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
                        if ( has_post_thumbnail() ) {
                            $img = wp_get_attachment_image_src( $tn_id, array(9999,$height) );
                            the_post_thumbnail();
                                                    }
                        endwhile; else:
                    endif;
                    //wp_reset_postdata();  //commented out because I don't know if I need it
                    $offset = $offset+1;
                    $column = 0;
                    $rowWidth = 0;
                }
            }
        endwhile; else:
    ?>
4

0 回答 0