【问题标题】:How to output thumbnail twice in a WordPress loop如何在 WordPress 循环中输出两次缩略图
【发布时间】:2014-12-26 05:50:14
【问题描述】:

好的,我有一个投资组合页面。每个投资组合项目都附有缩略图。我想要的是从第一篇文章中打印出第一个缩略图两次。一个在开头,另一个在结尾。比如:image_1, image_2, image_3, image_4 & 然后是 image_1。

我的代码是:

<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>

我该怎么做?

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    正如我之前的回答 here,我会使用 PHP for。但是,如果由于某种原因您不希望这样做,我认为您不需要为此使用两个循环。您可以执行以下操作:

    <?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>
    

    【讨论】:

    • 缩略图应该在“portfolio-box”div之外!
    • 但是我从你的代码中得到了一些想法......让我试试,如果我遇到任何问题,我会告诉你的!非常感谢您的帮助!
    • 是的,我已经使用您的部分代码解决了我的问题。我只需要这两行:'$first = $loop->posts[0]; ID );?>' 非常感谢您的帮助。
    • 编辑了我的答案以反映您最新的 cmets。
    【解决方案2】:

    这里是代码,根据你的需要编辑它:

    $(document).ready(function()
    {
       var imagesrc = $(".example post's image class").attr("src");
       $(".the container or post after which you want to put your image").appendTo("<img src='"+imgsrc+"' alt='image' />");
    });
    

    【讨论】:

      猜你喜欢
      • 2017-12-13
      • 1970-01-01
      • 2012-04-02
      • 2014-11-13
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      相关资源
      最近更新 更多