dongzu0742 2014-12-25 14:40
浏览 104
已采纳

如何在WordPress循环中输出缩略图两次

ok I have a portfolio page. Every portfolio item has thumbnail attached. what I want is to print out the first thumbnail from the first post twice. One at the beginning & other at the ending. like: image_1, image_2, image_3, image_4 & then image_1 again.

my code is:

<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;">
<?php $args = array( 'post_type' => 'portfolio', 'order' => 'dsc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php $extraFirstClass = $loop->current_post == '0' ? ' main-image-porfolio-main' : ''; ?>

<?php 
$attributes = array(
    "class" => "main-image portfolio " . $extraFirstClass,
    "id" => "photo_{$post->ID}",
);
the_post_thumbnail("portfolio_thumb", $attributes);
?>

<?php endwhile; ?> 

<?php rewind_posts(); ?> 


    <div class="portfolio-box">
        <h5>Portfolio</h5>
        <ul class="item-list" id="portfolio-list">
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>" rel="<?php echo $post->ID; ?>"><?php the_title(); ?></a>
        </li>
        <?php endwhile; ?>              
        </ul>
    </div>

</div>

How can I do it?

  • 写回答

2条回答 默认 最新

  • doubi12138 2014-12-25 15:51
    关注

    As previously answered by me here, I would use PHP for. However, if for some reason you don't want that, I don't think you would need to resort to two loops for this. You can do the following:

    <?php
    $args = array( 'post_type' => 'portfolio', 'order' => 'dsc');
    $loop = new WP_Query( $args );
    $first = $loop->posts[0];
    $attributes = array(
        "class" => "main-image portfolio",
        "id" => "photo_{$loop->posts[$i]->ID}",
    );
    
    <div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;">
        for( $i = 0; $i < count($loop->posts); $i++ ) {
            $attrs = $attributes;
            $attrs["class"] .= $i === 0 ? ' main-image-portfolio-main' : '';
            echo get_the_post_thumbnail( $loop->posts[$i]->ID, "portfolio_thumb", $attrs );
        }
        echo get_the_post_thumbnail( $first->ID, "portfolio_thumb", $attrs );
        ?>
    
        <div class="portfolio-box">
            <h5>Portfolio</h5>
            <ul class="item-list" id="portfolio-list">
    
                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <li>
                    <a href="<?php the_permalink(); ?>" rel="<?php echo $post->ID; ?>"><?php the_title(); ?></a>            
                </li>
                <?php endwhile; ?>
    
                <li>
                    <a href="<?php echo get_permalink($first->ID);?>" rel="<?php echo $first->ID; ?>"><?php echo $first->post_title;?></a>
                </li>  
            </ul>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的