我正在使用下面的代码通过WordPress循环挑选最新的帖子,但它在full
尺寸:
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<!-- Post Start -->
<div id="post">
<a class="post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ):
the_post_thumbnail(\'full\');
else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
</a>
<h2 class="post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post_desc"><?php the_excerpt(); ?></span>
<div class="clear"></div>
</div>
<!-- Post Start -->
<?php endwhile;
else: ?>
<p class="error"><?php _e(\'Sorry, No Article Is Available Here...\'); ?></p>
<?php endif;
我想挑选
full
循环的第一个帖子的图像,以及
thumbnail
循环中所有其他帖子的图像。我可以通过CSS做到这一点,但这对SEO不友好。我正在使用以下代码,但它不起作用:
$first = true;
foreach ( $obj as $value ) {
if ( $first ) { ?>
<!-- Post Start -->
<div id="post">
<a class="post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ):
the_post_thumbnail(\'full\');
else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
</a>
<h2 class="post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post_desc"><?php the_excerpt(); ?></span>
<div class="clear"></div>
</div>
<!-- Post Start -->
<?php $first = false;
} else { ?>
<!-- Post Start -->
<div id="post">
<a class="post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ):
the_post_thumbnail(\'thumbnail\');
else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
</a>
<h2 class="post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post_desc"><?php the_excerpt(); ?></span>
<div class="clear"></div>
</div>
<!-- Post Start -->
<?php }
}
最合适的回答,由SO网友:Nathan Fitzgerald - Fitzgenius 整理而成
试着用传统的$i
迭代器(未测试),但我不明白为什么这不起作用。。
<?php
$i = 0;
while ( have_posts() ) : the_post();
$i++;
if ( $i == 1 ): ?>
<!-- First Post -->
<div id="post">
<a class="post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(\'full\'); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
</a>
<h2 class="post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post_desc"><?php the_excerpt(); ?></span>
<div class="clear"></div>
</div>
<?php else: // else $i == 1 ?>
<!-- All Other Posts -->
<div id="post">
<a class="post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(\'thumbnail\'); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
</a>
<h2 class="post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post_desc"><?php the_excerpt(); ?></span>
<div class="clear"></div>
</div>
<?php endif; // endif for $i == 1 ?>
<?php endwhile; ?>