用图片显示新闻三小一大(循环)

时间:2015-01-16 作者:user3766356

我试着把4条新闻和图片放在一起,3个小图片和一个大图片,已经显示出来了,但是“大”和最后一个“小”重复出现

enter image description here

<?php
    $b=1; 
    $args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'gens\', 
                \'field\' => \'slug\', 
                \'terms\' => \'newsgen\' 
            )
        ),
        \'post_type\'=>\'\',    //add your post type name 
        \'posts_per_page\' => 4, 
        \'orderby\' => \'asc\', 
    );
        query_posts($args);
        while ( have_posts() ) : the_post(); 
    ?>

    <?php 
    if($b%4==1 && $b==1) : ?>
    <div id="news-big">
    <?php
    $image_url = catch_that_image();
    $image = thumb($image_url, 209, 97);
    ?>
    <a href="<?php the_permalink(); ?>">
    <img src="<?php echo $image[\'url\']; ?>"/></a>
    </div>
    <?php endif; ?>
    <a href="<?php the_permalink(); ?>"><div class="news-small">
    <div class="container-small">
    <?php
    $image_url = catch_that_image();
    $image = thumb($image_url, 136, 75);
    ?>
    <a href="<?php the_permalink(); ?>">
    <img src="<?php echo $image[\'url\']; ?>"/></a>
    </div>
    <!-- End Thumb Container -->

    <?php
        $b++;   
        endwhile; 
        wp_reset_query();
    ?>

2 个回复
SO网友:Rohil_PHPBeginner

我不知道你为什么把$b%4==1 条件

您只需检查$b==1.还有一件事我建议你if and else 二者都所以代码应该是这样的:

 <?php
    $b=1; 
    $args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'gens\', 
                \'field\' => \'slug\', 
                \'terms\' => \'newsgen\' 
            )
        ),
        \'post_type\'=>\'\',    //add your post type name 
        \'posts_per_page\' => 4, 
        \'orderby\' => \'asc\', 
    );
        query_posts($args);
        while ( have_posts() ) : the_post(); 
    ?>

    <?php 
        if($b==1) : //change in condition
    ?> 
        <div id="news-big">
        <?php
        $image_url = catch_that_image();
        $image = thumb($image_url, 209, 97);
        ?>
        <a href="<?php the_permalink(); ?>">
        <img src="<?php echo $image[\'url\']; ?>"/></a>
        </div>
    else: //else is added here
        <a href="<?php the_permalink(); ?>"><div class="news-small">
        <div class="container-small">
        <?php
        $image_url = catch_that_image();
        $image = thumb($image_url, 136, 75);
        ?>
        <a href="<?php the_permalink(); ?>">
        <img src="<?php echo $image[\'url\']; ?>"/></a>
        </div>
        <!-- End Thumb Container -->
        </div>
        <!-- End of news-small div -->  
    <?php endif; ?>
    <?php
        $b++;   
        endwhile; 
        wp_reset_query();
?>
尝试以上内容,并让我知道输出。

SO网友:Vincent Wong

我猜你有4条不同的新闻,你想显示第一条新闻使用每个页面的大图像,而大图像的新闻与其他新闻不同,对吗?如果你是对的,下面的代码,可能对你有用

<?php
$b=1; 
$args = array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'gens\', 
            \'field\' => \'slug\', 
            \'terms\' => \'newsgen\' 
        )
    ),
    \'post_type\'=>\'\',    //add your post type name 
    \'posts_per_page\' => 4, 
    \'orderby\' => \'asc\', 
);
    query_posts($args);
    while ( have_posts() ) : the_post(); 
?>

<?php 
if($b==1) : ?> //change here
<div id="news-big">
<?php
$image_url = catch_that_image();
$image = thumb($image_url, 209, 97);
?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $image[\'url\']; ?>"/></a>
</div>
<?php else: ?>  //change here
<a href="<?php the_permalink(); ?>"><div class="news-small">
<div class="container-small">
<?php
$image_url = catch_that_image();
$image = thumb($image_url, 136, 75);
?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $image[\'url\']; ?>"/></a>
</div>
<!-- End Thumb Container -->

<?php
    endif; //add code
    $b++;   
    endwhile; 
    wp_reset_query();
?>

结束