【问题标题】:WordPress: Alternating thumbnail sizesWordPress:交替缩略图大小
【发布时间】:2015-11-19 23:31:04
【问题描述】:

在概览页面上,我只想显示带有文章链接的帖子缩略图。但拇指应该有不同的大小(5 个大小,交替)。类似的东西:

图片1:拇指大小1

图片 2:拇指大小2

图片 3:thumbsize3

图片 4:thumbsize4

图片 5:thumbsize5

图 6:thumbsize1

图片 7:thumbsize2

等等。

我知道此代码在均匀和不均匀的帖子之间有所不同:

<?php $i = 1; // Index setzen ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php if ($i % 2 == 1 ) {
   echo 'CONTENT';
} ?>

<?php if ($i % 2 == 0 ) { 
echo 'CONTENT';     
}
$i++; // Index um 1 erhoehen
?>
<?php endwhile; ?>
<?php endif; ?> 

那么如何更改此代码以满足我的需要?

更新:

这是我现在的代码:

<?php  
$thumbname = 'thumbsize'.($i%5+1); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( $thumbname ); ?></a>
<?php $i++; // Index um 1 erhoehen
?> 


<?php endwhile; ?>
<?php endif; ?> 

在我的functions.php中:

add_image_size( 'thumbsize2', 300, 350, true );
add_image_size( 'thumbsize3', 750, 350, true );
add_image_size( 'thumbsize4', 400, 350, true );
add_image_size( 'thumbsize5', 300, 350, true );
add_image_size( 'thumbsize6', 350, 350, true );

等等。 但是,缩略图具有完整的上传大小。

【问题讨论】:

    标签: php wordpress thumbnails


    【解决方案1】:

    如果名称后缀为 1 到 5,您可以使用以下方法确定名称。

    <?php $i = 1; // Index setzen ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php  
    $thumbname = 'thumbsize'.($i%5);
    //use thumbname accordingly in content here.
    the_post_thumbnail( $thumbname ); 
    $i++; // Index um 1 erhoehen
    ?>
    <?php endwhile; ?>
    <?php endif; ?> 
    

    【讨论】:

    • 谢谢,这似乎是一个合适的解决方案。但是,我不确定在哪里添加我的链接和图片来源:''
    • 抱歉,找到了。但它不能正常工作。大概是我的一些错。请参阅上面我编辑的问题。
    • 在您的实现中,我可以看到 thumbname 作为字符串而不是变量传递。在第三行将其更新为the_post_thumbnail( $thumbname ),它应该可以工作
    • 现在几乎完美了。图 1-5 是拇指大小 1-5。但是,图像 6 是全尺寸的。然后,图像 7 是 thumbsize1,图像 8 是 thumbsize2 等等。只是中间的图像尺寸错误。我在 php 循环中尝试了不同的变体,但我无法弄清楚...
    • 嗯,这是一个完全独立的辩论,请查看this
    【解决方案2】:

    考虑这个例子:

    <?php $i = 0; // Index setzen ?>
    <?php if (have_posts()) : ?> 
    <div class="parent">
    <?php while (have_posts()) : the_post(); ?>
    <?php 
       // Close parent Div after completing 5 child and create 
       // New parent Div
       if ( $i != 0 && $i % 5 == 0 ) {
          echo '</div><div class="parent">';
       } 
    ?>
    <div class="child <?php echo $i ?>"></div>
    <?php $i++; ?>
    <?php endwhile; ?>
    </div><!-- // End Parent -->
    <?php endif; ?> 
    
    // Output
    <div class="parent">
        <div class="child 0"></div>
        <div class="child 1"></div>
        <div class="child 2"></div>
        <div class="child 3"></div>
        <div class="child 4"></div>
    </div>
    <div class="parent">
        <div class="child 5"></div>
        <div class="child 6"></div>
        <div class="child 7"></div>
        <div class="child 8"></div>
        <div class="child 9"></div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多