我在问如何在最近上传的最新帖子中添加自定义div元素。我该怎么做?
我的代码:
<ul class="entrybox">
<?php
$args = array(\'post_type\' => \'portfolio\', \'posts_per_page\' => -1, \'paged\' => $paged);
$loop = new WP_Query($args);
$count = 0;
?>
<?php if ($loop) : while ($loop->have_posts()) : $loop->the_post(); ?>
<li class="grid_4 portfolio-post">
<?php if ($count == 1) echo \'<div class="newest">NEW</div>\'; ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<header class="post-thumb">
<?php the_post_thumbnail(\'thumbnail-portfolio\'); ?>
</header><!-- End header.post-thumb -->
<aside>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</aside><!-- End aside -->
</a>
</li><!-- End li.grid_4 portfolio-post -->
<?php endwhile; ?>
<?php else : ?>
<p>No portfolio items were found! I\'m not sure what you\'re looking for.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul><!-- End ul.entrybox -->
我尝试过:
<?php if ($count == 1) echo \'<div class="newest">NEW</div>\'; ?>
但这是行不通的。
希望有人能解决!
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
好当然不行-$count
是未定义的,您永远不会更改它的值。
您可能想要这样的东西:
<?php if ($loop) : while ($loop->have_posts()) : $loop->the_post(); ?>
<li class="grid_4 portfolio-post">
<?php if ($loop->current_post == 0) echo \'<div class="newest">NEW</div>\'; ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<header class="post-thumb">
<?php the_post_thumbnail(\'thumbnail-portfolio\'); ?>
</header><!-- End header.post-thumb -->
<aside>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</aside><!-- End aside -->
</a>
</li><!-- End li.grid_4 portfolio-post -->
<?php endwhile; ?>
<?php else : ?>
<p>No portfolio items were found! I\'m not sure what you\'re looking for.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul><!-- End ul.entrybox -->
WP_Query
已经有自己的柜台了(
current_post
), 所以你不必定义你自己的。